Search code examples
ioswkwebview

Why is WKWebView not opening links with target="_blank"?


WKWebView does not open any links which have target="_blank" a.k.a. 'Open in new Window' attribute in their HTML <a href>-Tag.


Solution

  • My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar behavior like UIWebView which always open new window in the current frame.

    Implement the WKUIDelegate delegate and set it to _webview.uiDelegate. Then implement:

    - (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
    {
      if (!navigationAction.targetFrame.isMainFrame) {
        [webView loadRequest:navigationAction.request];
      }
    
      return nil;
    }