wkwebviewwkwebviewconfiguration

loadHTMLString for a simple WKWebView crashed at runtime with error WebPageProxy::processDidTerminate: (pid 0), reason 3


I'm trying to work with a simple WKWebView with a single button, which upon clicking should callback to the Swift via JavaScript. However, I see the following log in Xcode console and the view loads empty WKWebView:

  • [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason 3
  • [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason = 3
  • [pageProxyID=5, webPageID=6, PID=84636] WebPageProxy::processDidTerminate: (pid 84636), reason 3
  • [pageProxyID=5, webPageID=6, PID=84636] WebPageProxy::dispatchProcessDidTerminate: reason = 3
  • [pageProxyID=5, webPageID=6, PID=84636] WebPageProxy::tryReloadAfterProcessTermination: process crashed and the client did not handle it, not reloading the page because we reached the maximum number of attempts

My code to initialize the WKWebView and setting up the callback looks like so:

   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)
   }

Solution

  • Took a while to understand that — because my app is sandboxed — I needed to enable the Network connections under Signing & Capabilities, which in turn add the proper entitlements to the project that launches the WKWebView with the much awaited My Button. I also now get the callback when clicking this button!

    Capabilities to enable

    Sample App Launched with button

    Hope this helps someone who's in this same situation.