IOS 14 DeviceOrientationEvent 立即被拒绝

问题描述 投票:0回答:1

我正在尝试访问 iPhone 加速计。从 iOS 13 开始,您必须在应用程序中请求许可。我使用的是 IOS 14,但我什至没有收到请求。马上就被拒绝了

if (typeof(DeviceMotionEvent) !== 'undefined' && typeof(DeviceOrientationEvent.requestPermission) === "function") {
    // requestAccess()
}

function requestAccess() {
    DeviceOrientationEvent.requestPermission()
        .then(permissionState => {
            text(permissionState,100, 100);  // answer is "denied"
            if (permissionState === 'granted') {
            window.addEventListener('deviceorientation', () => {});
            }
        })
        .catch(console.error);
   }
javascript accelerometer ios14 motionevent motion-detection
1个回答
1
投票

requestAccess()
函数需要由用户的操作触发,例如作为按钮
onclick
事件的回调函数。这是一个演示:https://accelerometerdemo.netlify.app

<button onclick="requestAccess()">Get Accelerometer Permissions</button>
function requestAccess() {
    DeviceMotionEvent.requestPermission().then(permissionState => {
        if (permissionState == 'granted') {
            // ...
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.