Using Javascript from SpiderBasic
Dirk Geppert
 
Posts: 180
Joined: Fri Sep 22, 2017 7:02 am

iOS get permisson for motion sensors

by Dirk Geppert Mon Dec 16, 2019 3:00 pm

Since iOS 13 it is necessary, to get the permissions from user to be authorized to interrogate the motion sensors.
Otherwise things like VR view and Babylon will not work anymore..
I found an working javascript example @ https://github.com/aframevr/aframe/issues/4287

But how can I integrate this as a function in spiderbasic?

Code: Select all
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)
    })
}


Any help is welcome!
Dirk Geppert
 
Posts: 180
Joined: Fri Sep 22, 2017 7:02 am

Re: iOS get permisson for motion sensors

by Dirk Geppert Wed Dec 18, 2019 10:09 am

If no one can help with the SB integration.
It works at least as pure Javascript.
The code generates a clickable banner and an onClick unlocks the sensors.

How can I remove this button/banner?

Just to delete the banner child does not work.. :?

Code: Select all
document.querySelector('body').deleteChild(banner)
Dirk Geppert
 
Posts: 180
Joined: Fri Sep 22, 2017 7:02 am

Re: iOS get permisson for motion sensors

by Dirk Geppert Wed Dec 18, 2019 12:17 pm

Even better: A-Frame has it now directly in the framework :D

https://github.com/aframevr/aframe
Return to Javascript

Who is online

Users browsing this forum: No registered users and 0 guests