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