xcode 如何强制在 WebView 中单击的任何链接在 Safari 中打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28574272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I force any links clicked within a WebView to open in Safari?
提问by Kian Cross
I have a WebView in my application and I would like any links clicked within the WebView to open in Safari (instead of the WebView itself).
我的应用程序中有一个 WebView,我希望在 WebView 中单击的任何链接在 Safari 中打开(而不是 WebView 本身)。
I am developing the application in Swift.
我正在 Swift 中开发应用程序。
What is the best method to do this?
执行此操作的最佳方法是什么?
回答by Ronald Martin
This is done essentially the same way in Swift as in Obj-C:
这在 Swift 中的完成方式与 Obj-C 中的基本相同:
First, declare that your view controller conforms to UIWebViewDelegate
首先,声明您的视图控制器符合 UIWebViewDelegate
class MyViewController: UIWebViewDelegate
Then implement webViewShouldStartLoadingWithRequest:navigationType:
in your View Controller:
然后webViewShouldStartLoadingWithRequest:navigationType:
在您的视图控制器中实现:
// Swift 1 & 2
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .LinkClicked:
// Open links in Safari
UIApplication.sharedApplication().openURL(request.URL)
return false
default:
// Handle other navigation types...
return true
}
}
// Swift 3
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .linkClicked:
// Open links in Safari
guard let url = request.url else { return true }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// openURL(_:) is deprecated in iOS 10+.
UIApplication.shared.openURL(url)
}
return false
default:
// Handle other navigation types...
return true
}
}
Finally, set your UIWebView
's delegate, e.g., in viewDidLoad
or in your Storyboard:
最后,设置您UIWebView
的委托,例如,viewDidLoad
在您的故事板中或中:
webView.delegate = self
回答by Femi Loki
Updated for swift 3
为 swift 3 更新
func webView(_: UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.linkClicked {
UIApplication.shared.open(shouldStartLoadWith.url!, options: [:], completionHandler: nil)
return false
}
return true
}
回答by jefflovejapan
You need to implement the method webViewShouldStartLoadingWithRequest:navigationType
on your web view's delegate and look for the links you want to open in Safari. If you send those off to the OS with [[UIApplication sharedApplication]openURL:]
they will open in Safari.
您需要webViewShouldStartLoadingWithRequest:navigationType
在 Web 视图的委托上实现该方法并查找要在 Safari 中打开的链接。如果您将它们发送到操作系统,[[UIApplication sharedApplication]openURL:]
它们将在 Safari 中打开。
回答by Cesar Bielich
Updated for swift 4.2
为 swift 4.2 更新
func webView(_: UIWebView, shouldStartLoadWith: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
if navigationType == UIWebView.NavigationType.linkClicked {
UIApplication.shared.open(shouldStartLoadWith.url!, options: [:], completionHandler: nil)
return false
}
return true
}