简体   繁体   English

如何通过 Javascript 检查 iOS.12.2+ 中的运动/方向权限状态?

[英]How to check motion/orientation permissionState in iOS.12.2+ by Javascript?

With iOS12.2+ Apple wants a user gesture to activate devicemotion or deviceorientation.在 iOS12.2+ 中,Apple 希望通过用户手势来激活 devicemotion 或 deviceorientation。 I have found samples in the Internet, fe here .我在互联网上找到了样本,fe here I show a popup window in which the user will be informed and can accept or deny the access.我会显示一个弹出窗口,用户将在其中收到通知并可以接受或拒绝访问。 This is all working fine with Javascript.这一切都适用于 Javascript。

I'm aware Safari is caching the permissionState (granted or denied), so after giving or denying permission (by user) the page can be reloaded and the permissionState is still known.我知道 Safari 正在缓存权限状态(授予或拒绝),因此在授予或拒绝权限(由用户)后,页面可以重新加载,并且权限状态仍然是已知的。 Here comes the problem: (how) can I check if the permission is already granted or denied?问题来了:(如何)我可以检查权限是否已被授予或拒绝? In case it's already set I don't want to display the popup (asking for permission) again after page reload.如果它已经设置,我不想在页面重新加载后再次显示弹出窗口(请求许可)。

okay, I think I found a solution, it's not pretty but it works.好吧,我想我找到了一个解决方案,它不是很好,但它有效。

First I have a function that checks if iOS 13+ if that's the case, I then request permissions, turns out if the user has already granted access DeviceMotionEvent.requestPermission returns granted if they haven't granted access it fails, and then I trigger the modal.首先我有一个函数来检查 iOS 13+ 是否是这种情况,然后我请求权限,如果用户已经授予访问权限DeviceMotionEvent.requestPermission返回授予如果他们没有授予访问权限失败,然后我触发模态。 To then ask for permissions again.然后再次请求权限。

DeviceMotionEvent.requestPermission()
.then(response => {
    if (response) {
        console.log(response)
    }
})
.catch(function(error) {
    console.log("error");
    // Trigger modal to ask for permissions
    $('#askForPermission').modal('toggle')
});

hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS 12.2上的safari中使用JavaScript请求动态访问 - Request motion access with JavaScript in safari on iOS 12.2 检测是否在Javascript中启用了运动和方向访问-iOS - Detect whether Motion & Orientation Access is enabled in Javascript - iOS CMattitude在JavaScript中用于iOS运动事件吗? - CMattitude in JavaScript for iOS motion events? iOS 12.2:即使在设置中手动启用后,设备方向数据仍然被阻止 - iOS 12.2: device orientation data still blocked even after manual enabling in settings iOS Safari - 检查是否授予设备运动权限 - iOS safari - check if permission granted for device motion 如何使用JavaScript检查图像的方向? - How can i check for image's orientation using javascript? iOS Safari的iframe无法获取设备方向和运动事件 - iOS Safari's iframe doesn't get device orientation and motion events 在最新的 iOS 更新到 12.2 后,如何修复 iOS 主屏幕 Chrome 快捷方式中的“whatsapp://”协议点击中断? - How to fix the "whatsapp://" protocol click in iOS Homescreen Chrome Shortcut broken after latest iOS update to 12.2? 检测设备运动/方向支持? - Detecting device motion/orientation support? 是否检测iOS相对于JavaScript中的“主页”按钮的方向? - Detect iOS orientation relative to the home button in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM