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

2.8k Views Asked by At

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

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

We had a similar error in our application. Our use case was that we were using webView to render HTML websites and then taking snapshots of it. For each render, we were creating a new WKWebView instance.

Users reported that as more and more renders happened, it became slower and slower. We witnessed that after many creations of WKWebView, it would fail with a message in the console:

WebPageProxy::dispatchProcessDidTerminate: reason=Crash

If we tried to create a new WKWebView after that and take a snapshot of it, snapshot completion would be called after ±10 minutes with an error code 1, stating that the error is unknown.

The solution was one of the following:

  • Clear the cache (WKWebsiteDataStore.default().removeData) after we receive a delegate call in webViewWebContentProcessDidTerminate or before creating a new WKWebView
  • For some reason, it also works if you do not create a new instance of WKWebView and instead re-use the same one