Jed*_*ant 7 ios swift wkwebview
我试图让使用斯威夫特和WKWebviews打开,有一个链接我的混合iOS应用程序target="_blank",或者如果URL中包含http://,https://或mailto:在移动Safari浏览器.
从这个答案我得到这个代码.
func webView(webView: WKWebView!, createWebViewWithConfiguration configuration: WKWebViewConfiguration!, forNavigationAction navigationAction: WKNavigationAction!, windowFeatures: WKWindowFeatures!) -> WKWebView! {
if navigationAction.targetFrame == nil {
webView.loadRequest(navigationAction.request)
}
return nil
}
Run Code Online (Sandbox Code Playgroud)
首先,这对我没有任何作用.其次,我希望它在新窗口中打开.我发现这个代码应该做那样的事情......
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}
Run Code Online (Sandbox Code Playgroud)
我如何把这两个放在一起让它们起作用?我需要添加什么来使ViewController声明工作?
Jed*_*ant 11
在(从这里)
override func loadView() {
super.loadView()
self.webView.navigationDelegate = self
self.webView.UIDelegate = self //must have this
}
Run Code Online (Sandbox Code Playgroud)
然后添加功能(从这里,添加)...
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
}
Run Code Online (Sandbox Code Playgroud)
首先添加WKNavigationDelegate和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 shared = UIApplication.sharedApplication()
let urlString = url!.absoluteString
if shared.canOpenURL(url!) {
shared.openURL(url!)
}
decisionHandler(WKNavigationActionPolicy.Cancel)
}
decisionHandler(WKNavigationActionPolicy.Allow)
}
Run Code Online (Sandbox Code Playgroud)
针对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
}
Run Code Online (Sandbox Code Playgroud)