从iOS 11开始,我在Safari上使用devicemotion事件来访问陀螺仪。
历史。
问题 :
在iOS 13.4上,我这边没有修改代码,在我所有使用该功能的项目中,即使用户同意陀螺仪访问,rotationRate也总是为空。
我搜索了苹果的官方答案,但没有任何可用的答案。它看起来像一个bug,但也许我这边的实现是错误的?
很高兴知道我的代码。
首先,我监听devicemotion事件(rotationRate为null),当界面准备好,用户点击一个按钮,陀螺仪访问要求和rotationRate有值。
有什么好办法吗?
这似乎是iOS 13.4中的一个bug,因为连 webxr polyfill 未能检测到哪个能力有设备,因为他们有(第3770行)。
if (this.isWithoutDeviceMotion) {
window.addEventListener('deviceorientation', this.onDeviceOrientationCallback_);
} else {
window.addEventListener('devicemotion', this.onDeviceMotionCallback_);
}
该条件评估为false,所以使用devicemotion,然后... rotationRate
和 accelerationIncludingGravity
都是空的。
所以我认为从现在开始的解决方法是监听devicemotion事件而不是devorientation事件,即使事先已经申请了权限,但这是必须的。