我有一个很好的DeviceMotionEvent请求,所有的工作都在Safari (或其他需要权限的浏览器)上,大致是这样的:
// on click of a button
DeviceMotionEvent.requestPermission()
.then(response => {
if (response == 'granted') {
// do my thing
}
})
.catch(function(error) {
console.log(error);
// do my other thing
});
这很有效。但当用户转到新页面时,他们必须再次请求权限。显然,我再次调用'requestPermission‘,所以他们当然会这样做。
如何确定是否已授予权限?或者权限是按页授予的(而不是基于会话/站点)?
我可以将响应存储在本地存储或其他地方,但我更喜欢类似以下内容的内容:
DeviceMotionEvent.permissionStatus
还是别的什么?
发布于 2021-02-25 11:48:59
您应该能够使用设备情感eventListener检查是否已授予权限。记住,你必须按一个按钮或类似的按钮才能运行DeviceMotionEvent.requestPermission()
例如:
let hasOrientationControls = false;
window.addEventListener("devicemotion", () => {
hasOrientationControls = true;
});
// then when the button is pressed we can request permissions if we need
onButtonPressed () => {
if (hasOrientationControls) return;
else DeviceMotionEvent.requestPermission();
}
我还使用了这个
isVRReady = window.DeviceOrientationEvent && "ontouchstart" in window;
https://stackoverflow.com/questions/59050840
复制相似问题