简单 WKWebView 的 loadHTMLString 在运行时崩溃,错误为 WebPageProxy::processDidTerminate: (pid 0),原因 3

Vin*_*eri 2 wkwebview wkwebviewconfiguration

我正在尝试使用一个带有单个按钮的简单 WKWebView,单击该按钮后应通过 JavaScript 回调到 Swift。但是,我在 Xcode 控制台中看到以下日志,并且视图加载空 WKWebView:

  • [pageProxyID=5,webPageID=6,PID=0] WebPageProxy::processDidTerminate: (pid 0),原因 3
  • [pageProxyID=5,webPageID=6,PID=0] WebPageProxy::dispatchProcessDidTerminate:原因 = 3
  • [pageProxyID=5,webPageID=6,PID=84636] WebPageProxy::processDidTerminate:(pid 84636),原因 3
  • [pageProxyID=5,webPageID=6,PID=84636] WebPageProxy::dispatchProcessDidTerminate:原因 = 3
  • [pageProxyID=5,webPageID=6,PID=84636] WebPageProxy::tryReloadAfterProcessTermination:进程崩溃并且客户端没有处理它,没有重新加载页面,因为我们达到了最大尝试次数

我初始化 WKWebView 并设置回调的代码如下所示:

   override func viewDidLoad() {
        super.viewDidLoad()
        let config = WKWebViewConfiguration()
        config.userContentController.add(self, name: "callback")
        wkWebView = WKWebView(frame: self.view.frame, configuration: config)
        wkWebView.autoresizingMask = [.height, .width]
        self.view.addSubview(wkWebView)
        
        let htmlStr = "<!DOCTYPE html><html><body><button type=\"button\" onclick=\"myFunction()\">My Button</button><script type=\"text/javascript\">function myFunction() { window.webkit.messageHandlers.callback.postMessage('clicked!'); }</script></body></html>"
        self.wkWebView.loadHTMLString(htmlStr, baseURL: Bundle.main.resourceURL)
   }
Run Code Online (Sandbox Code Playgroud)

Vin*_*eri 6

花了一段时间才明白 \xe2\x80\x94\xc2\xa0 因为我的应用程序是沙盒的 \xc2\xa0\xe2\x80\x94 我需要在签名和功能下启用网络连接,这反过来又添加了适当的权利该项目推出了 WKWebView 以及期待已久的My Button. 现在,单击此按钮时我也会收到回调!

\n

启用的功能

\n

使用按钮启动示例应用程序

\n

希望这对处于同样情况的人有所帮助。

\n