question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make dialog to call DeviceMotionEvent.requestPermission in iOS 13+

See original GitHub issue

8th wall dialog is a good example to follow (open link on iOS 13)

https://apps.8thwall.com/8w/jini/

requestPermision has to be called in a handler triggered by a user gesture

  buttonEl.addEventlistener('click', function () {
    DeviceMotionEvent.requestPermission().then(response => {
      if (response == 'granted') {
        window.addEventListener('devicemotion', (e) => {
        // do something with e
      })
    }
  }).catch(console.error)
 });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:23 (16 by maintainers)

github_iconTop GitHub Comments

9reactions
dav1appcommented, Oct 31, 2019

So, here is what I think:

The DeviceMotion permission is something that the developer using the platform should deal with. It means that aframe itself should not be responsible to make this request.

Although, I think that we should do add the banner as an option to be inserted when aframe loads and use it in the exemples provided. This will help developers to get things done.

I’ve done this, based on the home website. I’m also making the IOS 12 version, with the step-by-step on how enable it.

possible-banner

Here is the code (still polishing): https://codepen.io/davimello28/pen/MWWOKar

For the lazy productive developers

Here is a full working exemple:

window.onload = function () {

  // Check if is IOS 13 when page loads.
  if ( window.DeviceMotionEvent && typeof window.DeviceMotionEvent.requestPermission === 'function' ){

      // Everything here is just a lazy banner. You can do the banner your way.
      const banner = document.createElement('div')
      banner.innerHTML = `<div style="z-index: 1; position: absolute; width: 100%; background-color:#000; color: #fff"><p style="padding: 10px">Click here to enable DeviceMotion</p></div>`
      banner.onclick = ClickRequestDeviceMotionEvent // You NEED to bind the function into a onClick event. An artificial 'onClick' will NOT work.
      document.querySelector('body').appendChild(banner)
  }
}


function ClickRequestDeviceMotionEvent () {
  window.DeviceMotionEvent.requestPermission()
    .then(response => {
      if (response === 'granted') {
        window.addEventListener('devicemotion',
          () => { console.log('DeviceMotion permissions granted.') },
          (e) => { throw e }
      )} else {
        console.log('DeviceMotion permissions not granted.')
      }
    })
    .catch(e => {
      console.error(e)
    })
}
3reactions
dmarcoscommented, Nov 19, 2019
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to requestPermission for devicemotion and ...
To request permission, we would just need to call a method on the DeviceMotionEvent or DeviceOrientationEvent : requestPermission It ...
Read more >
How to Request Device Motion and Orientation Permission in ...
Learn how to Request Device Motion and Orientation Permission in iOS 13. ... Just be sure to call the associated requestPermission() method from...
Read more >
mobile safari - DeviceMotionEvent.requestPermission() throws ...
I have a web page that I'm loading in Safari on iOS 13.4.1. The web page calls ...
Read more >
ios 13 and DeviceOrientationEvent.requestPermission
I'm now attempting to trigger the permission dialog at startup and I can't seem to get it to fire without linking it to...
Read more >
Unable to check DeviceOrientationEvent - Apple Developer
requestPermission () .then(permissionState => { if (permissionState ... else { // handle regular non iOS 13+ devices console.log ("not iOS"); } }.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found