【问题标题】:WebKit userContentController:didReceiveScriptMessage: not getting called for iOS 13WebKit userContentController:didReceiveScriptMessage:iOS 13 未调用
【发布时间】:2020-03-16 04:34:05
【问题描述】:

我正在使用 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' });

请让我知道是否有人遇到过这个问题以及一些可能的解决方案。

【问题讨论】:

    标签: javascript ios swift webkit wkwebview


    【解决方案1】:

    经过大量搜索,我发现了在 iOS 13 中 user agent 属性被更改的解决方案,我们在 WEB 中使用它来检查它是否是移动的。 isMobile 检查失败,因为我们的消息处理程序没有被调用。

    iOS 13 中进行了许多重大更改,目前我们需要注意这些更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      相关资源
      最近更新 更多