Force a WebView link to launch Safari?(强制 WebView 链接启动 Safari?)
问题描述
我的 iPhone 应用程序中嵌入了一个 UIWebView.我希望能够将该 web 视图中的某些链接打开到完整的 Mobile Safari 应用程序(即不是我的嵌入式版本).
I have a UIWebView embedded within an iPhone app of mine. I want to be able to have certain links within that webview open into the full Mobile Safari app (i.e. not my embedded version of it).
是否有一种简单的方法来构建我的一些 href 来强制执行此操作,而不是在我的嵌入式 web 视图中打开每个链接?
Is there a simple way to structure some of my hrefs to force this, instead of every link opening within my embedded webview?
谢谢.
推荐答案
为了扩展 Randy 所说的,这是我在我的应用程序中使用的每个 http://、https://和 mailto://在外部 Safari 或 Mail 应用程序中打开的 URL:
To expand upon what Randy said, this is what I use in my application to make every http://, https://, and mailto:// URL open in the external Safari or Mail applications:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSURL *requestURL =[ [ request URL ] retain ];
if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ])
&& ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) {
return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ];
}
[ requestURL release ];
return YES;
}
正如 Randy 所说,您将希望在您设置为 UIWebView 委托的任何类中实现此功能.要仅选择 URL 启动 Safari,您可以将其方案从 http://更改为 safari://或类似的东西,并且仅将这些 URL 踢到系统(在将自定义 URL 方案替换为 http://之后).
As Randy says, you'll want to implement this within whatever class you set to be the delegate of the UIWebView. To have only select URLs launch Safari, you could change their scheme from http:// to safari://, or something similar, and only kick those URLs off to the system (after replacing the custom URL scheme with http://).
我在我的内部帮助文档中执行此操作,该文档是在 UIWebView 中显示的 HTML,因此在我的应用程序中嵌入通用 Web 浏览器时,我不会在审核过程中遇到问题.
I do this within my internal help documentation, which is HTML displayed in a UIWebView, so that I don't run into issues in the review process with having a general-purpose web browser embedded in my application.
这篇关于强制 WebView 链接启动 Safari?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:强制 WebView 链接启动 Safari?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01