3 回答

TA贡献2065条经验 获得超14个赞
if ( location.protocol != "https:" ) {
location.href = "https:" + window.location.href.substring( window.location.protocol.length );
}
function permission () {
if ( typeof( DeviceMotionEvent ) !== "undefined" && typeof( DeviceMotionEvent.requestPermission ) === "function" ) {
// (optional) Do something before API request prompt.
DeviceMotionEvent.requestPermission()
.then( response => {
// (optional) Do something after API prompt dismissed.
if ( response == "granted" ) {
window.addEventListener( "devicemotion", (e) => {
// do something for 'e' here.
})
}
})
.catch( console.error )
} else {
alert( "DeviceMotionEvent is not defined" );
}
}
const btn = document.getElementById( "request" );
btn.addEventListener( "click", permission );
使用页面上的元素作为事件触发器并为其指定“请求”ID。
这将检查 https 并在请求 API 授权之前根据需要进行更改。昨天找到这个,但不记得网址。

TA贡献1735条经验 获得超5个赞
从 iOS 13 beta 2 开始,您需要调用 DeviceOrientationEvent.requestPermission() 来访问陀螺仪或加速度计。这将显示一个权限对话框,提示用户允许此站点的运动和方向访问。
请注意,如果您尝试在页面加载时自动调用它,这将不起作用。用户需要采取一些操作(如点击按钮)才能显示对话框。
此外,当前的实现似乎要求该站点启用 https。

TA贡献1817条经验 获得超6个赞
您需要单击或用户手势来调用 requestPermission()。像这样 :
<script type="text/javascript">
function requestOrientationPermission(){
DeviceOrientationEvent.requestPermission()
.then(response => {
if (response == 'granted') {
window.addEventListener('deviceorientation', (e) => {
// do something with e
})
}
})
.catch(console.error)
}
</script>
<button onclick='requestOrientationPermission();'>Request orientation permission</button>
注意:如果您在权限提示上单击取消并想再次测试它,您需要退出 Safari 并重新启动它以让提示返回。
添加回答
举报