WKWebView and window.open

Bret picture Bret · Oct 17, 2015 · Viewed 14.6k times · Source

It appears that a lot of links on websites use window.open in their onclick handlers but WKWebView seems to completely ignore window.open.

Is there a workaround for this?

I tried setting javaScriptCanOpenWindowsAutomatically preference to true but that didn't seem to help

Answer

Stefan Arentz picture Stefan Arentz · Oct 18, 2015

When a web application calls window.open() in JavaScript, the WKWebView will call the - webView:createWebViewWithConfiguration:forNavigationAction:windowFeatures: method on its UIDelegate.

In that delegate method you should create a new WKWebView with the WKWebViewConfiguration that is given to you. If you present this new WKWebView on screen, it will load with the correct content.

This is documented in the WKUIDelegate documentation, although it is not very explicit that this is called as a result of window.open().