强制 WebView 链接启动 Safari? [英] Force a WebView link to launch Safari?

查看:19
本文介绍了强制 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆