我有一个非常简单的UIWebView,其中包含我的应用程序包中的内容.我希望Web视图中的任何链接都在Safari中打开,而不是在Web视图中打开.这可能吗?
将其添加到UIWebView委托:
(编辑以检查导航类型.您还可以传递file://
相关链接的请求)
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (navigationType == UIWebViewNavigationTypeLinkClicked ) { [[UIApplication sharedApplication] openURL:[request URL]]; return NO; } return YES; }
Swift版本:
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication().openURL(request.URL!) return false } return true }
Swift 3版本:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.linkClicked { UIApplication.shared.openURL(request.url!) return false } return true }
更新
正如openURL
iOS 10中已弃用的那样:
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool { guard let url = request.url, navigationType == .linkClicked else { return true } UIApplication.shared.open(url, options: [:], completionHandler: nil) return false }
如果有人想知道,Drawnonward的解决方案在Swift中会是这样的:
func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool { if navigationType == UIWebViewNavigationType.LinkClicked { UIApplication.sharedApplication().openURL(request.URL) return false } return true }
对user306253的回答快速评论:当你尝试在UIWebView中自己加载某些内容时(即使是代码中),请注意这一点,这种方法可以防止它发生.
你可以做些什么来防止这种情况(感谢韦德):
if (inType == UIWebViewNavigationTypeLinkClicked) { [[UIApplication sharedApplication] openURL:[inRequest URL]]; return NO; } return YES;
您可能还想处理UIWebViewNavigationTypeFormSubmitted
和UIWebViewNavigationTypeFormResubmitted
类型.
其他答案有一个问题:它们依赖于您执行的操作而不依赖于链接本身来决定是在Safari中还是在Webview中加载它.
现在有时这正是你想要的,这很好; 但有些时候,特别是如果你的页面中有锚链接,你真的只想打开Safari中的外部链接,而不是内部链接.在这种情况下,您应该检查URL.host
您的请求的属性.
我使用那段代码来检查我是否在正在解析的URL中有一个主机名,或者它是否嵌入了html:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { static NSString *regexp = @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])[.])+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp]; if ([predicate evaluateWithObject:request.URL.host]) { [[UIApplication sharedApplication] openURL:request.URL]; return NO; } else { return YES; } }
您当然可以调整正则表达式以满足您的需求.
在Swift中,您可以使用以下代码:
extension YourViewController: UIWebViewDelegate { func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool { if let url = request.url, navigationType == UIWebView.NavigationType.linkClicked { UIApplication.shared.open(url, options: [:], completionHandler: nil) return false } return true } }
确保检查URL值和navigationType.