0

是否可以检查是否已授予 DeviceMotionEvent 权限?以及我们如何将请求警报文本从“本地主机想要访问运动和方向”更改为“应用程序名称想要访问运动和方向”?

public requestDeviceMotion() {
    if typeof (DeviceMotionEvent as any).requestPermission === 'function') {
      (DeviceMotionEvent as any).requestPermission()
        .then(permissionState => {
          if (permissionState === 'granted') {
            window.addEventListener('devicemotion', () => { });
          }
        })
        .catch(console.error);
    } else {
      // handle regular non iOS 13+ devices
      console.log("not iOS");
    }
 }
4

1 回答 1

0

您能否澄清“是否可以检查”的含义?

您的代码可用于此类目的。即如果未授予权限状态。

请注意,如果“常规非 iOS13+ 设备”也具有 DeviceMotionEvent,则不会触发您的 else 块。为此,您应该像这样使用 Platform.is:

public requestDeviceMotion() {
   if (typeof DeviceMotionEvent.requestPermission === 'function') {
     DeviceMotionEvent.requestPermission().then(permissionStatus => {
       if (permissionState === 'granted') {
        // window.addEventListener('devicemotion', () => { });
        // I commented this out because you should not do this.
        // This will cause a memory leak. You should make the listener
        // a function which can then be removed in destroy lifecycle event
        // but that is outside the scope of your question
       }
     }

   } else if (!this.platform.is('ios')) {
     console.log('not ios');
   }

}

至于自定义消息,据我所知,这只是iOS的一个选项。

将以下内容添加到 Ionic 项目文件夹底部的 config.xml 中。注意:您可能只需要添加实际的配置文件条目,但为了清楚起见,会显示完整的对象

<platform name="ios">
   <config-file parent="NSMotionUsageDescription" target="*-Info.plist">
     <string>This is your new message</string>
   </config-file>
</platform>

于 2021-08-17T11:41:29.357 回答