UIWebView在Safari中打开链接

我有一个非常简单的UIWebView,包含我的应用程序包中的内容。我想任何链接在web视图中打开Safari而不是在web视图。这可能吗?

138552 次浏览

将这个添加到UIWebView委托:

(经过编辑以检查导航类型。你也可以传递file://请求,这将是相对链接)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}


return YES;
}

斯威夫特版本:

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
}

Swift 4版本:

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
}

< em > < / em >更新

由于openURL在iOS 10中已弃用:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[request URL] options:@{} completionHandler:nil];
return NO;
}


return YES;
}

对user306253的回答的一个快速评论:小心,当你自己尝试在UIWebView中加载一些东西时(即即使从代码中加载),这个方法将防止它发生。

你能做什么来防止这种情况(感谢韦德):

if (inType == UIWebViewNavigationTypeLinkClicked) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}


return YES;

你可能还想处理UIWebViewNavigationTypeFormSubmittedUIWebViewNavigationTypeFormResubmitted类型。

其他答案有一个问题:它们依赖于你所做的操作,而不是链接本身来决定是在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;
}
}

当然,您可以调整正则表达式以满足您的需要。

如果有人想知道,Drawnonward的解决方案在斯威夫特中是这样的:

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
if navigationType == UIWebViewNavigationType.LinkClicked {
UIApplication.sharedApplication().openURL(request.URL)
return false
}
return true
}

在我的例子中,我想确保web视图中的所有东西都能打开Safari,除了初始加载,所以我使用。

- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if(inType != UIWebViewNavigationTypeOther) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
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。

下面是Xamarin iOS版的drawnonward的答案。

class WebviewDelegate : UIWebViewDelegate {
public override bool ShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType) {
if (navigationType == UIWebViewNavigationType.LinkClicked) {
UIApplication.SharedApplication.OpenUrl (request.Url);
return false;
}
return true;
}
}

公认的答案行不通。

如果你的页面通过Javascript加载url, navigationType将是UIWebViewNavigationTypeOther。不幸的是,这也包括后台页面加载,如分析。

为了检测页面导航,你需要比较[request URL][request mainDocumentURL]

这个解决方案将在所有情况下工作:

- (BOOL)webView:(UIWebView *)view shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)type
{
if ([[request URL] isEqual:[request mainDocumentURL]])
{
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
else
{
return YES;
}
}

应用拒绝

最后,UIWbView已经死了,苹果将不再接受它。

苹果开始向所有仍在使用UIWebView的应用程序所有者发送电子邮件:

弃用API使用——苹果将停止接受使用UIWebView API的应用程序提交。

苹果非常重视用户隐私,很明显他们不会允许不安全的webview

所以要尽快从你的应用中移除UIWebView。不要使用尝试使用__ABC0在新创建的应用程序和我更喜欢使用WKWebView如果可能的话

ITMS-90809:废弃的API使用-苹果将停止接受 提交使用UIWebView api的应用程序。看到 https://developer.apple.com/documentation/uikit/uiwebview获取更多信息 信息。< / p >

例子:

import UIKit
import WebKit


class WebInfoController: UIViewController,WKNavigationDelegate {


var webView : WKWebView = {
var webview = WKWebView()
return webview
}()


var _fileName : String!


override func viewDidLoad() {
self.view.addSubview(webView)
webView.fillSuperview()
let url = Bundle.main.url(forResource: _fileName, withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}




func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
print(error.localizedDescription)
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("Strat to load")
}
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
print("finish to load")
}
}

UIWebViewUIWebViewDelegate已弃用。你将不被允许将更新推送到Appstore。参考

使用WKWebViewWKNavigationDelegate

示例代码:

class YourClass: WKNavigationDelegate {


override public func viewDidLoad() {
super.viewDidLoad()
let webView = WKWebView()
webView.navigationDelegate = self
self.view.addaddSubview(webView)
}
    

public func webView(_ webView: WKWebView,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let cred = URLCredential(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential, cred)
}


public func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
    

self.webView.load(navigationAction.request)
decisionHandler(.allow)
}
}