IOS 14 DeviceOrientationEvent gets denied right away

784 Views Asked by At

I am trying to get access to the iPhones Accelerometer. Since iOS 13 You have to ask for Permission in App. I am using IOS 14 and I don't even get an request. It gets denied right away

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

function requestAcces(){
    DeviceOrientationEvent.requestPermission()
        .then(permissionState => {
            text(permissionState,100, 100);  // answer is "denied"
            if (permissionState === 'granted') {
            window.addEventListener('deviceorientation', () => {});
            }
        })
        .catch(console.error);
   }
1

There are 1 best solutions below

0
Shuqiao Zhang On

The requestAccess() function needs to be triggered by the user's action, for example, as a callback function for button onclick event. Here is a demo: https://accelerometerdemo.netlify.app

<button onclick="requestAccess()">Get Accelerometer Permissions</button>
function requestAccess() {
    DeviceMotionEvent.requestPermission().then(permissionState => {
        if (permissionState == 'granted') {
            // ...
        }
    }
}