首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >跨页面存储DeviceOrientationEvent和DeviceMotionEvent权限

跨页面存储DeviceOrientationEvent和DeviceMotionEvent权限
EN

Stack Overflow用户
提问于 2019-11-26 20:18:31
回答 3查看 163关注 0票数 2

我有一个很好的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

还是别的什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-11-30 19:45:04

我认为您唯一的选择是构建一个单页面应用程序,并使用history.pushState()要在浏览器中更新位置,请在您希望“更改页面”时执行此操作。

票数 1
EN

Stack Overflow用户

发布于 2019-12-06 16:59:28

编辑:您可以使用`Web存储[Web Storage API]具有以下两种机制:

  • sessionStorage
  • localStorage

顾名思义,这两个接口非常适合于基于会话的数据,或者如果您希望在连接关闭后仍然保持状态。

票数 0
EN

Stack Overflow用户

发布于 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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59050840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档