WKWebView iOS 13 中的 DeviceMotion 和 DeviceOrientation

Nik*_*rya 1 ios devicemotion wkwebview ios-permissions

我想在 iOS 13+ 中使用DeviceMotion和DeviceOrientation事件WKWebView。我正在使用下面的代码

<html>
    <style>
        #container{
            margin: 20px; 
            padding: 20px
        }
        #deviceType{
            text-align: center; 
            font-family: Arial, Helvetica, sans-serif; 
            font-size: 3rem;
            font-weight: bold; 
            color: #08f
        }
        #permission{
            padding: 2rem 5rem;
            background: #08f;
            color: white; 
            margin-top: 30;
            font-family: Arial, Helvetica, sans-serif; 
            font-size: 3rem;
            border: none; 
            border-radius: 1rem
        }
        #permissionStatus{
            text-align: center; 
            font-family: Arial, Helvetica, sans-serif; 
            font-size: 2.5rem;
            font-weight: 600; 
            color: red;
            margin-top: 30px
        }
    </style>
    <body>
        <div id="container">
                <div id="deviceType"></div>
                <div style="text-align: center">
                    <button id="permission">Ask for permission</button>
                </div>
                <div id="permissionStatus">Pending</div>
        </div>
    </body>
    <script>

        if (typeof DeviceMotionEvent.requestPermission === 'function') {
            document.getElementById('deviceType').innerText = 'iOS 13'
            document.getElementById('permission').addEventListener('click', function () {
                console.log('button clicked')
                DeviceMotionEvent.requestPermission().then(response => {

                    // This is showing "denied" without showing any permission popup
                    console.log(response)
                    document.getElementById('permissionStatus').innerText = response

                }).catch(console.error)
            });
        } else {
            // ignore this case for processing
            console.log('non iOS 13')
            document.getElementById('deviceType').innerText = 'non iOS 13'
        }
    </script>
</html>
Run Code Online (Sandbox Code Playgroud)

该网页可在 Safari 和 Chrome 中运行,并请求使用设备运动的许可,如下所示

图片:

在 Chrome 上请求许可

状态将根据我单击“允许”或“取消”而更新

在此输入图像描述

但是在我的应用程序内部,我正在使用WKWebView,当我按下“询问权限”按钮时,它没有显示任何询问权限的警报,而是直接将状态设置为“拒绝”。(如下所示)。

在此输入图像描述

如果我做错了什么或者需要任何额外的东西,请指导我WKWebView。这是我的 iOSWKWebView代码。我不知道是否需要更改WKUserContentController或更改。WKWebViewConfiguration

self.webView = WKWebView(frame: .zero)
self.webView.translatesAutoresizingMaskIntoConstraints = false

// Add the webview as a subview of MTKView
self.view.addSubview(self.webView)

self.webView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
self.webView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
self.webView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
self.webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true


let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
let date = Date(timeIntervalSince1970: 0)
WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })

self?.webView.load(URLRequest(url: URL(string: url)!))
Run Code Online (Sandbox Code Playgroud)

Nik*_*rya 5

我尝试使用uiDelegate并且有效

只需在里面添加一行viewDidLoad

webView.uiDelegate = self
Run Code Online (Sandbox Code Playgroud)

此外,您还必须确认ViewControllerto的类型WKUIDelegate。就是这样