Abra un enlace WKWebview target = "_ blank" en Safari

Estoy intentando get mi aplicación Hybrid IOS que utiliza Swift y WKWebviews para abrir un enlace que tiene target="_blank" o si la URL contiene http:// , https:// o mailto: en Mobile Safari.

De esta respuesta obtengo este código.

 func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! { if navigationAction.targetFrame == nil { webView.loadRequest(navigationAction.request) } return nil } 

Primero, eso no hace nada por mí. Segundo, quiero que se abra en una nueva window. Y encontré este código que debe hacer algo así …

 if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") { UIApplication.shanetworkingApplication().openURL(requestUrl) } 

¿Cómo juntar estos dos y hacer que funcionen? ¿Qué debo agregar a la statement ViewController para que funcione?

En ( desde aquí )

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

Luego agregue la function ( desde aquí, con adiciones ) …

 func webView(webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration, forNavigationAction navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if navigationAction.targetFrame == nil { var url = navigationAction.request.URL if url.description.lowercaseString.rangeOfString("http://") != nil || url.description.lowercaseString.rangeOfString("https://") != nil || url.description.lowercaseString.rangeOfString("mailto:") != nil { UIApplication.shanetworkingApplication().openURL(url) } } return nil } 

Primero agregue WKNavigationDelegate y webviewWk.navigationDelegate = self

 func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) { //this is a 'new window action' (aka target="_blank") > open this URL externally. If we´re doing nothing here, WKWebView will also just do nothing. Maybe this will change in a later stage of the iOS 8 Beta if navigationAction.navigationType == WKNavigationType.LinkActivated { println("here link Activated!!!") let url = navigationAction.request.URL let shanetworking = UIApplication.shanetworkingApplication() let urlString = url!.absoluteString if shanetworking.canOpenURL(url!) { shanetworking.openURL(url!) } decisionHandler(WKNavigationActionPolicy.Cancel) } decisionHandler(WKNavigationActionPolicy.Allow) } 

El código de Jed en 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.shanetworking.openURL(url) } } return nil } 
 func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { if navigationAction.targetFrame == nil, let url = navigationAction.request.url, let scheme = url.scheme { if ["http", "https", "mailto"].contains(where: { $0.caseInsensitiveCompare(scheme) == .ordenetworkingSame }) { UIApplication.shanetworking.openURL(url) } } return nil }