LoginSignup
5
4

More than 3 years have passed since last update.

iOS12.2よりdevicemotion, deviceorientationがデフォルトで取得できなくなったことと、その検知方法

Posted at

iOS12.2より、safariに「モーションと画面の向きのアクセス」項目が追加され、デフォルトでOFFの状態となったため、devicemotionイベント等を取得することができなくなりました。

設定はどうやって変えるの?

設定>Safariから「モーションと画面の向きのアクセス」項目が追加され、デフォルトでOFF
をONにすることで

どうやって検知したらいい?

ググると

if('ondevicemotion' in window) {}

的なコードが出てくるんですが、実際にiOS12.2の設定をOFFにしていても検知してくれません。

const id = setTimeout(() => {
                 //ここに動かない時にさせたい挙動を書く
             }, 500);
             window.addEventListener('devicemotion', (e) => clearTimeout(id));

このようなコードをかくことで、
つまり、デバイスオリエンテーションイベントが発生したときにタイマー処理をしているものをクリアしてキャンセル=>動かない時だけsetTimeout()の中身が実行される、ということができるようです。

参考

https://www.macrumors.com/2019/02/04/ios-12-2-safari-motion-orientation-access-toggle/
https://github.com/w3c/deviceorientation/issues/57#issuecomment-481039307

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4