打开WKWebview target =" _blank"在Safari中链接 [英] Open a WKWebview target="_blank" link in Safari
问题描述
我正在尝试使用Swift和WKWebviews打开包含 target =_ blank
的链接的混合IOS应用程序,或者如果URL包含<$ c $移动设备中的c> http / code>, https://
或 mailto:
Safari。
I am trying to get my Hybrid IOS app that uses Swift and WKWebviews to open a link that has target="_blank"
or if the URL contains http://
, https://
, or mailto:
in 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
}
首先,这对我没有任何作用。其次,我希望它在新窗口中打开。我发现这个代码应该做类似的事情......
First, that doesn't do anything for me. Second, I want it to open in a new window. And I found this code that is supposed to do something like that...
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}
如何将这两者放在一起让它们起作用?我需要在ViewController声明中添加什么才能使其正常工作?
How do I put these two together and get them to work? What do I need to add to the ViewController declaration to make it work?
推荐答案
为iOS 10更新的代码Swift 3:
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
}
这篇关于打开WKWebview target =" _blank"在Safari中链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!