5

I have a WKWebView in my application, there are some buttons inside the WKWebView on clicking on it, a new page is not loaded. By the look of it, they are trying to open an about_blank page. In the browser, the link is opening in a new page and the URL is loaded.

BXV
  • 405
  • 3
  • 16

3 Answers3

9

The way that you do this is by implementing WKUIDelegate and override this method

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView?

If you return a new WKWebView with the passed configuration, the request will be loaded to a new webview. Since target=_blank tries to open a new window in browser, this method compensates for that in iOS. You could launch a new UIViewController with this new WKWebView where the request for target=_blank page.

Here is a simple example, where I substitute the main WKWebView with new webView, and load the request in same viewcontroller,

extension ViewController: WKNavigationDelegate, WKUIDelegate {

    public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        let newWebView = WKWebView(frame: self.webView.frame,
                         configuration: configuration)
        view.addSubview(newWebView)
        return newWebView
    }
}

This in someway works like a tab in browser. But, bear in mind that with the change as above, forward and backward do not work, since you have created a new webview. If you want everything to work, I suggest that you load a new viewcontroller with this webview or create a new view where this would be loaded and shown, and you could switch to other main webview.

Sandeep
  • 20,908
  • 7
  • 66
  • 106
  • Also, this is useful https://stackoverflow.com/questions/25713069/why-is-wkwebview-not-opening-links-with-target-blank – timaktimak Feb 11 '21 at 07:39
5

I went with this approach, even though this approach opens the URL in a device safari browser, anyway those links are pointing to external links, this method helped

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.targetFrame == nil {
    if let url = navigationAction.request.url {
        let app = UIApplication.shared
        if app.canOpenURL(url) {
            app.open(url, options: [:], completionHandler: nil)
        }
    }
}
decisionHandler(.allow)
}
BXV
  • 405
  • 3
  • 16
2

Swift Solution

public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        
        if navigationAction.targetFrame == nil {
            self.webView.load(navigationAction.request)
            }
        return nil
    }

You need to delegate WKUIDelegate

Ucdemir
  • 2,852
  • 2
  • 26
  • 44