Open a Wkwebview Target="_Blank" Link in Safari

WKWebView target=_blank link open new tab in safari ios11, swift 4

I have pasted some sample code of a WKWebView project (load local html from folder) that requires links that have target=_blank to open in a new browser window.

I have highlighted the 3 things you must have to get links to open correctly.

  1. class ViewController extends WKUIDelegate
  2. self.webView.uiDelegate = self
  3. Use UIApplication.shared.open instead of webView.load

Let me know it it works and if anyone can suggest improvements to the sample code below, that would help me too :)

Full sample code for Xcode 9.2, Swift 4 below.

Good Luck

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.uiDelegate = self

let htmlPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "www")
let htmlUrl = URL(fileURLWithPath: htmlPath!)
let htmlDir = Bundle.main.url(forResource: "www", withExtension: nil)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlDir!)
}

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil {
//webView.load(navigationAction.request)
UIApplication.shared.open(navigationAction.request.url!, options: [:])
}
return nil
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override var prefersStatusBarHidden: Bool {
return true
}

}

Open a WKWebview target=_blank link in Safari

Code updated for iOS 10 Swift 3:

override func loadView() {
super.loadView()
self.webView.navigationDelegate = self
self.webView.uiDelegate = self //must have this
}

func webView(_ webView: WKWebView,
createWebViewWith configuration: WKWebViewConfiguration,
for navigationAction: WKNavigationAction,
windowFeatures: WKWindowFeatures) -> WKWebView? {
if navigationAction.targetFrame == nil, let url = navigationAction.request.url {
if url.description.lowercased().range(of: "http://") != nil ||
url.description.lowercased().range(of: "https://") != nil ||
url.description.lowercased().range(of: "mailto:") != nil {
UIApplication.shared.openURL(url)
}
}
return nil
}

iOS14, WKWebView cannot open links with _blank target

At the moment of writing this, WKWebView has some "unknown" bug with parsing the _blank target attributes of href tag.

It seems that Safari works properly when the target is _self.

The idea is to replace all _blank values with _self.

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
let jsCode = "var allLinks = document.getElementsByTagName('a');if (allLinks) { var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var target = link.getAttribute('target');if (target && target == '_blank') {link.setAttribute('target','_self');} } }"
webView.evaluateJavaScript(jsCode, completionHandler: nil)
}

Why is WKWebView not opening links with target=_blank?

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

WKWebView Open _Bank Target Links in Safari

This is what eventually worked for me. Seems to open any url (including javascript type popovers used in YouTube) in Safari instead of opening them in WKWebView (or not opening them at all).

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
{
NSLog(@"createWebViewWithConfiguration %@ %@", navigationAction, windowFeatures);
if (!navigationAction.targetFrame.isMainFrame) {
[[NSWorkspace sharedWorkspace] openURL:[navigationAction.request URL]];
}
return nil;
}


Related Topics



Leave a reply



Submit