我有一个很好的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
还是别的什么?
发布于 2019-11-30 19:45:04
我认为您唯一的选择是构建一个单页面应用程序,并使用history.pushState()
要在浏览器中更新位置,请在您希望“更改页面”时执行此操作。
发布于 2019-12-06 16:59:28
编辑:您可以使用`Web存储[Web Storage API
]具有以下两种机制:
sessionStorage
localStorage
顾名思义,这两个接口非常适合于基于会话的数据,或者如果您希望在连接关闭后仍然保持状态。
发布于 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
复制相似问题