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

查看:337
本文介绍了强制WebView链接启动Safari?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程序中嵌入了一个UIWebView。我希望能够将该webview中的某些链接打开到完整的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来强制这个,而不是在我的嵌入式webview中打开每个链接?

Is there a simple way to structure some of my hrefs to force this, instead of every link opening within my embedded webview?

谢谢。

推荐答案

为了扩展Randy所说的内容,我在我的应用程序中使用这个来在外部Safari中打开每个http://,https://和mailto:// 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; 
}

正如兰迪所说,你要在你设定的任何课程中实现这个成为UIWebView的委托。要只有选择的URL启动Safari,您可以将他们的方案从http://更改为safari://或类似的东西,并且只将这些URL发送到系统(在用http://替换自定义URL方案之后) 。

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天全站免登陆