gpt4 book ai didi

ios - UIWebView 在 Safari 中打开链接

转载 作者:IT王子 更新时间:2023-10-29 07:24:57 26 4
gpt4 key购买 nike

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

最佳答案

将此添加到 UIWebView 委托(delegate):

(编辑以检查导航类型。您也可以通过 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
}

更新

由于 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;
}

关于ios - UIWebView 在 Safari 中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2899699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com