ios 13 DeviceOrientationEvent.requestPermission: how to force device to ask again for user permission
Asked Answered
M

4

10

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
    }
}
Mightily answered 24/9, 2019 at 16:35 Comment(0)
C
4

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

Crosswise answered 15/2, 2020 at 14:43 Comment(2)
Is this still the most direct way to reset the prompt? This is a cumbersome instruction to provide to a user who accidentally taps "Cancel" on the prompt.Regan
I would store their state (whether they hit Ok or Cancel). If they cancelled, then from time to time you remind them "Hey, for full functionality, you should allow us to detect sensor events!" and then trigger the requestPermission prompt.Aardwolf
P
3

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.

Probable answered 30/9, 2019 at 10:13 Comment(3)
It hadn't worked form me. Shall website be on https?Duelist
I have local network IP with some port and it didn't work for me. I tried to request permissions and safari always returns denied on iPhone.Duelist
Yes, you need to be on httpsUncouth
S
0

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 compiled and it's taking me a while to reverse engineer their logic.

Sybyl answered 24/9, 2019 at 20:32 Comment(4)
Yes I've tried on Safari iOS 13 (iPhone 8) and there it worked as expected (I could grant or deny access). The problem is I can't ask again for a different choice once user made a decision (grant or deny).Mightily
Gotcha, thanks. I'll let you know if I see anything regarding the caching issue. It seems like my issue was that I was using codepen and the permission API doesn't like to be run inside an iframe - makes sense.Sybyl
I tested this in iOS 13.4.1, in both Safari and Chrome (v81.0.4044.124). In both, I get "denied" immediately after pressing the button.Disgorge
@LukeStevenson you need to have https, it's always "denied" on localhost :)Spiccato
R
0

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

Recto answered 4/4, 2022 at 2:0 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Cassirer

© 2022 - 2024 — McMap. All rights reserved.