Asking for a permission on iOS 12.2+

Hi guys,

I am working with the gyroscope and since iOS 12.2 you’ll need to ask for a permission:
“DeviceMotionEvent.requestPermission()”

Anybody found a working way the get this running, calling from inside the playcanvas app?

1 Like

Something like this? (Found on japanese website)

cv._start.on(“click”,function(){
cv._start.hide();
DeviceOrientationEvent.requestPermission().then(res => {
if(res===“granted”){
alert (“go for it”);
}else{
isGyro=false;
alert (“no permission”);
}
});
});

This one seems to be a possible solution, but not sure about the implementation:

1 Like

Hey Pepone,

I’ve had to do this many many times since I’ve started working with web/playcanvas, here’s a sample snippet with the code you referenced in the context of a playcanvas script- you just need the code from the post you linked to above, attached to a user input.

var IosPermission = pc.createScript('iosPermission');

IosPermission.attributes.add('testButton', {type: 'entity'});

// initialize code called once per entity
IosPermission.prototype.initialize = function() {
    this.testButton.element.on('click', this.AskPermission, this);
};

IosPermission.prototype.AskPermission = function() {
    // Enable device motion for iOS
    if (typeof DeviceMotionEvent.requestPermission === 'function') {
        DeviceMotionEvent.requestPermission()
            .then(permissionState => {                
            if (permissionState === 'granted') {
                window.addEventListener('devicemotion', () => {});

            } 
        })
            .catch(console.error);
    } else {

    }

    // Enable device orientation for iOS
    if (typeof DeviceOrientationEvent.requestPermission === 'function') {
        DeviceOrientationEvent.requestPermission()
            .then(permissionState => {
            if (permissionState === 'granted') {
                window.addEventListener('deviceorientation', (e) => {

                });
            }
        })
            .catch(console.error);
    } else {
        // handle regular non iOS 13+ devices
    }
};

I haven’t actually tested this code, but if you throw that script onto a gameobject and attach a button element, make a build and test on your ios device, it should work and pop the prompt.

Edit: Not 100% sure if you need a user input to prompt permissions, but thus far I’ve used it that way as iOS tends to need user input to start videos and such, would be worth proving out if you want it to pop up after something happens or when the page loads

1 Like