Jonny
Jonny

Reputation: 886

How to check motion/orientation permissionState in iOS.12.2+ by Javascript?

With iOS12.2+ Apple wants a user gesture to activate devicemotion or deviceorientation. I have found samples in the Internet, f.e. here. I show a popup window in which the user will be informed and can accept or deny the access. This is all working fine with Javascript.

I'm aware Safari is caching the permissionState (granted or denied), so after giving or denying permission (by user) the page can be reloaded and the permissionState is still known. Here comes the problem: (how) can I check if the permission is already granted or denied? In case it's already set I don't want to display the popup (asking for permission) again after page reload.

Upvotes: 1

Views: 904

Answers (1)

Peter Otto Kuhberg
Peter Otto Kuhberg

Reputation: 11

okay, I think I found a solution, it's not pretty but it works.

First I have a function that checks if iOS 13+ if that's the case, I then request permissions, turns out if the user has already granted access DeviceMotionEvent.requestPermission returns granted if they haven't granted access it fails, and then I trigger the modal. To then ask for permissions again.

DeviceMotionEvent.requestPermission()
.then(response => {
    if (response) {
        console.log(response)
    }
})
.catch(function(error) {
    console.log("error");
    // Trigger modal to ask for permissions
    $('#askForPermission').modal('toggle')
});

hope it helps

Upvotes: 1

Related Questions