Skip to content

Instantly share code, notes, and snippets.

@fumihumi
Last active October 25, 2019 17:19
Show Gist options
  • Save fumihumi/de86b925d368e3f04b271c5a300641e9 to your computer and use it in GitHub Desktop.
Save fumihumi/de86b925d368e3f04b271c5a300641e9 to your computer and use it in GitHub Desktop.
ios13's `DeviceMotionEvent.requestPermission` example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<p id="tmp">hogehoge</p>
</div>
<script>
// - [ ] https://dev.to/li/how-to-requestpermission-for-devicemotion-and-deviceorientation-events-in-ios-13-46g2
// - [ ] https://developer.mozilla.org/ja/docs/Web/API/DeviceMotionEvent
// - [ ] https://triple-underscore.github.io/deviceorientation-ja.html
const handleDeviseMotion = ( e ) => {
const { x, y, z } = e.acceleration;
if ( x > 10 || y > 10 || z > 10 ) {
console.log( '!' );
}
}
const onClick = () => {
console.log( 'clicked' );
// feature detect
if ( typeof DeviceMotionEvent.requestPermission === 'function' ) {
DeviceMotionEvent.requestPermission()
.then( permissionState => {
if ( permissionState === 'granted' ) {
window.addEventListener( 'devicemotion', handleDeviseMotion );
}
} )
.catch( console.error );
} else {
// handle regular non iOS 13+ devices
}
}
document.querySelector( '#tmp' ).addEventListener( 'click', onClick )
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment