Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
884 views
in Technique[技术] by (71.8m points)

ios - Is there a way to check permission granted status for DeviceOrientationEvent in javascript?

So I have a mobile 'AR' based page that can run on both ios and android devices. From what I understand IOS requires you to request permission from the user in order to assign the 'deviceorientation' event in the javascript.

I included this code, and it works on both devices now, however I was wondering if there was a way to see if the user has already given permission. I should only have to add the "click" event to check permission, if the user has not already granted them permission. For example if a user accept the permission, navigates away from the window and then back, it seems to work if I just assign 'deviceorientation' automatically. However I would like to only have to add the permission click event to request permission if permission hasnt already been granted.

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', onDeviceOrientationChangeEvent, false);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...