Ios 每次应用启动时都会调用WKWebView对DeviceMotion和DeviceOrientation的请求权限

Ios 每次应用启动时都会调用WKWebView对DeviceMotion和DeviceOrientation的请求权限,ios,swift,wkwebview,ios13,Ios,Swift,Wkwebview,Ios13,是否有办法只请求一次DeviceMotion和DeviceOrientation权限,而不是每次应用程序启动时。 我正在使用请求权限 webView.evaluateJavaScript("DeviceMotionEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('devicemotion', (e) => {})}}).catch(cons

是否有办法只请求一次DeviceMotion和DeviceOrientation权限,而不是每次应用程序启动时。 我正在使用请求权限

webView.evaluateJavaScript("DeviceMotionEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('devicemotion', (e) => {})}}).catch(console.error)")


那么,有没有一种方法可以始终授予权限

您应该使用
Userdefaults
来存储一些标志。请求权限后,在
Userdefaults
中为该标志设置一些值。当你的应用程序启动时,检查你是否设置了该标志。大概

    if UserDefaults.standard.bool(forKey: "openedOnce") {

    } else {
        webView.evaluateJavaScript("DeviceMotionEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('devicemotion', (e) => {})}}).catch(console.error)")
        webView.evaluateJavaScript("DeviceOrientationEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('deviceorientation', (e) => {})}}).catch(console.error)")
    }

    UserDefaults.standard.set(true, forKey: "openedOnce")
    UserDefaults.standard.synchronize()

应用程序发布意味着什么?当你的应用程序从后台转到前台,或者当你从终止或关闭状态打开应用程序时?终止或关闭。从后台,或者如果我用网络视图关闭屏幕并再次打开,则不会再次请求权限谢谢你的回答,但问题是应用程序需要权限,如果我跳过了它,运动检测就不起作用了,我想问题应该是,有没有一种方法总是授予许可
    if UserDefaults.standard.bool(forKey: "openedOnce") {

    } else {
        webView.evaluateJavaScript("DeviceMotionEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('devicemotion', (e) => {})}}).catch(console.error)")
        webView.evaluateJavaScript("DeviceOrientationEvent.requestPermission().then(response => { if (response == 'granted') {window.addEventListener('deviceorientation', (e) => {})}}).catch(console.error)")
    }

    UserDefaults.standard.set(true, forKey: "openedOnce")
    UserDefaults.standard.synchronize()