How to Request Device Motion and Orientation Permission in iOS 13

The Return of the Gyroscope

Lee Martin
Sep 19, 2019 · 2 min read

I’ve covered the issue of Apple turning device orientation and motion off by default since iOS 12.2 a few times on this Medium. Well, with iOS 13 finally reaching devices today, we now have a permission API we can use to gain access to these events, the same way we would the Camera or GPS. The permissions API is a known user pattern and it works great. 👍🏻

Integrating this into your web app couldn’t be simpler. Just be sure to call the associated requestPermission() method from a click event.

Here’s the code for devicemotion events:

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

And the equivalent for deviceorientation events:

DeviceOrientationEvent.requestPermission()
.then(response => {
if (response == 'granted') {
window.addEventListener('deviceorientation', (e) => {
// do something with e
})
}
})
.catch(console.error)

Simple enough. If you’re planning on providing fallbacks for non iOS 13 devices, you can simple check to see if the requestPermission method exists with a typeof call. Just swap DeviceMotionEvent with DeviceOrientationEvent as needed.

if (typeof DeviceMotionEvent.requestPermission === 'function') {
// iOS 13+
} else {
// non iOS 13+
}

I can’t wait to revisit my knighting app.

Thanks for reading!

🍏 Community around iOS development, mobile design, and marketing

Lee Martin

Written by

I develop websites for rock 'n' roll bands and get paid in sex and drugs. Previously Silva Artist Management, SoundCloud, and Songkick. Currently: Available

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

More from Flawless iOS

More from Flawless iOS

More from Flawless iOS

Improving your Swinject routine

Tim Kuzmin
Feb 7 · 4 min read

773

More from Flawless iOS

More from Flawless iOS

Tuples in Swift

652

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade