我正在使用WKWebView
在我的应用程序中显示Web内容,但无法在iOS 13设备上收到从JS
webkit
发送的消息。在以前的iOS版本上,它可以正常工作。
设置:
func setupWebView() {
let config: WKWebViewConfiguration = WKWebViewConfiguration()
config.userContentController.add(self, name: "mobileApp")
webView = WKWebView(frame: self.view.frame, configuration: config)
webView?.navigationDelegate = self
webView?.uiDelegate = self
webView?.scrollView.bounces = false
webView?.backgroundColor = UIColor.clear
self.view.addSubview(webView!)
self.webView?.load(URLRequest(url: URL(string: self.stringURL!)!))
}
WKScriptMessageHandler委托:
func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) {
let data = NSMutableDictionary(dictionary: message.body as! Dictionary)
//Method is not getting called
}
从JavaScript,我这样称呼它:
webkit.messageHandlers.mobileApp.postMessage({ 'backToApp': '1' });
[请让我知道是否有人遇到过此问题及其解决方案。
[经过大量搜索之后,我发现了以下解决方案:我们在user agent
中使用了iOS 13中的WEB
属性进行了更改,以检查其是否可移动。isMobile
检查失败,因为未调用我们的消息处理程序。
[iOS 13已完成许多重大更改,目前我们需要注意。