在Safari中打开WKWebview target =“_ blank”链接

我试图让我的混合IOS应用程序使用Swift和WKWebviews打开target="_blank"的链接,或者如果URL包含http://https://mailto:在Mobile Safari中。

从这个答案我得到这个代码。

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

首先,这对我没有任何帮助。 其次,我希望它在新窗口中打开。 我发现这个代码应该做这样的事情…

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

我怎么把这两个放在一起,让他们工作? 我需要添加到ViewController声明,使其工作?

代码更新为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 } 

在( 从这里 )

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

然后添加function( 从这里,添加 )…

 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.sharedApplication().openURL(url) } } return nil } 

首先添加WKNavigationDelegatewebviewWk.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 shared = UIApplication.sharedApplication() let urlString = url!.absoluteString if shared.canOpenURL(url!) { shared.openURL(url!) } decisionHandler(WKNavigationActionPolicy.Cancel) } decisionHandler(WKNavigationActionPolicy.Allow) } 
 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) == .orderedSame }) { UIApplication.shared.openURL(url) } } return nil }