最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ios 13 DeviceOrientationEvent.requestPermission: how to force device to ask again for user permission - Stack Overf

programmeradmin4浏览0评论

In iOS 13 Apple has introduced the API DeviceOrientationEvent.requestPermission. It must be triggered on user action (click, tap or equivalent). My problem here is that the result seems to be cached, so in case the user denies permission I can't ask access again (the promise is automatically fulfilled with the cached value). Is there any way to force the device to forgot the cached value and ask again for the user permission to access orientation data (I mean it should display again the popup window where the user can allow or deny access)?

This is the relevant code:

if (DeviceOrientationEvent && typeof(DeviceOrientationEvent.requestPermission) === "function") {
    const permissionState = await DeviceOrientationEvent.requestPermission();

    if (permissionState === "granted") {
        // Permission granted    
    } else {
        // Permission denied
    }
}

In iOS 13 Apple has introduced the API DeviceOrientationEvent.requestPermission. It must be triggered on user action (click, tap or equivalent). My problem here is that the result seems to be cached, so in case the user denies permission I can't ask access again (the promise is automatically fulfilled with the cached value). Is there any way to force the device to forgot the cached value and ask again for the user permission to access orientation data (I mean it should display again the popup window where the user can allow or deny access)?

This is the relevant code:

if (DeviceOrientationEvent && typeof(DeviceOrientationEvent.requestPermission) === "function") {
    const permissionState = await DeviceOrientationEvent.requestPermission();

    if (permissionState === "granted") {
        // Permission granted    
    } else {
        // Permission denied
    }
}
Share Improve this question asked Sep 24, 2019 at 16:35 revyrevy 4,72711 gold badges48 silver badges101 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Try simply to quit Safari and launch it back. The prompt will e back.

I see the same behavior on iOS 13 Safari. You need to remove the website data for the particular site that needs permission. Go to Settings > Safari > Advanced > Website data, lookup the site and remove all the data. Then the prompt should show up again when you ask for permission.

Are you sure it's a caching issue? I cannot get incognito Safari in iOS 13 to respond with a "granted" permission. I'm getting a "denied" response immediately - no prompt.

I'm using the following code to test the result, but it's always "denied" immediately without a prompt event showing. (https://codepen.io/ejarnutowski/pen/WNePqmM)

<button onclick="testDeviceOrientation()">Test Device Orientation</button>
function testDeviceOrientation() {
  if (typeof DeviceOrientationEvent !== 'function') {
    return setResult('DeviceOrientationEvent not detected')
  }
  if (typeof DeviceOrientationEvent.requestPermission !== 'function') {
    return setResult('DeviceOrientationEvent.requestPermission not detected')
  }
  DeviceOrientationEvent.requestPermission().then(function(result) {
    return setResult(result);
  });
}

function setResult(result) {
  document.getElementById('result').innerHTML = 'RESULT: ' + result;
}

Looks like drawmote.app has this working but the JS is piled and it's taking me a while to reverse engineer their logic.

Redirect it to another subdomain that points to the same page.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论