如何解决有没有办法在javascript中检查DeviceOrientationEvent的权限授予状态?
所以我有一个可以在 ios 和 android 设备上运行的基于移动“AR”的页面。据我了解,IOS 要求您向用户请求许可,以便在 javascript 中分配“deviceorientation”事件。
我包含了这段代码,现在它可以在两种设备上运行,但是我想知道是否有办法查看用户是否已经授予权限。如果用户尚未授予他们权限,我应该只需要添加“点击”事件来检查权限。例如,如果用户接受权限,离开窗口然后返回,如果我只是自动分配“设备方向”,它似乎可以工作。但是,如果尚未授予权限,我只想添加权限单击事件以请求权限。
function onDeviceOrientationChangeEvent(evt) {
//Do whatever
}
//Function to request permissions
function permission() {
// feature detect
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(permissionState => {
if (permissionState == 'granted') {
window.addEventListener('deviceorientation',(e) => {
// do something with e
onDeviceOrientationChangeEvent(e);
})
}
})
.catch(console.error)
} else {
// handle regular non iOS 13+ devices
window.addEventListener('deviceorientation',onDeviceOrientationChangeEvent,false);
}
}
//How can I only add the permission event,if the permission has yet to be granted?
if (/iPhone|iPad|iPod/i.test(navigator.userAgent)) {
// true for ios
window.addEventListener("click",permission);
}
//We add the event anyway because,if the user already was prompted previously
//And accepts,this will now work. If user is prompted and accepts,it still works
window.addEventListener('deviceorientation',false);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。