如何在Safari中打开外部链接,而不是应用程序的UIWebView? [英] How can I open an external link in Safari not the app's UIWebView?

查看:156
本文介绍了如何在Safari中打开外部链接,而不是应用程序的UIWebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Phonegap(cordova)应用程序,其中我想加载一些外部网页在phonegap WebView和我有其他外部网页,我想加载在safari当用户激活它们。



通常大多数人都有问题,他们想在WebView中打开一个外部链接。将 OpenAllWhitelistURLsInWebView 设置为(在Cordova.plist / Phongap.plist中)解决了该问题。





我希望我可以调用窗口

.open('http:// someexternalsite')在Safari和中打开window.parent.location.href ='http:// mysite'
解决方案



如果您要在safari中打开的链接都包含通用字符串,则可以使用下一段代码。

   - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{
NSURL * url = [request URL];

//拦截外部http请求并转发到Safari.app
//否则转发到PhoneGap WebView
if([[url scheme] isEqualToString:@SCHEME ]){
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
}

此代码放置在 AppDelegate.m 将打开在Safari中使用指定的SCHEME的所有URL。



恐怕是我能想出来的。



希望这有助于



UPDATE:



代码应放在MainViewControler中,至少对cordova 2.2。 0。



该方法最初被注释。我必须使用它来重定向Google地图链接:

  NSRange isGoogleMaps = [[url absoluteString] rangeOfString:@maps.google .comoptions:NSCaseInsensitiveSearch]; 
NSRange isGoogleTerms = [[url absoluteString] rangeOfString:@terms_maps.htmloptions:NSCaseInsensitiveSearch];

if(isGoogleMaps.location!= NSNotFound || isGoogleTerms.location!= NSNotFound){
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];


I have a Phonegap (cordova) application where I want to load some external webpages within the phonegap WebView and I have other external webpages that I want to load in safari when the user activates them.

Typically most people have the problem where they want to open an external link in the WebView. Setting OpenAllWhitelistURLsInWebView to YES (in Cordova.plist/Phongap.plist) solves that problem.

But I don't want to open all links the the WebView, just some.

I was hoping I could just call window.open('http://someexternalsite') to open in Safari and window.parent.location.href = 'http://mysite' to open it in the WebView.

Any idea how to do this?

解决方案

If the links you want to open in safari all contain a common string, you can use the next piece of code.

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request URL];

    // Intercept the external http requests and forward to Safari.app
    // Otherwise forward to the PhoneGap WebView
    if ([[url scheme] isEqualToString:@"SCHEME"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

This code placed in the AppDelegate.m will open all URL that use the specified SCHEME in Safari.

I'm afraid that is all I could come up with.

Hope this helps

UPDATE :

The code should be placed in the MainViewControler, at least for cordova 2.2.0.

The method is initially commented. I had to use it to redirect Google maps links :

NSRange isGoogleMaps = [[url absoluteString] rangeOfString:@"maps.google.com" options:NSCaseInsensitiveSearch];
NSRange isGoogleTerms = [[url absoluteString] rangeOfString:@"terms_maps.html" options:NSCaseInsensitiveSearch];

if(isGoogleMaps.location != NSNotFound || isGoogleTerms.location != NSNotFound ) {
        [[UIApplication sharedApplication] openURL:url];
       return NO;
}
else 
    return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];

这篇关于如何在Safari中打开外部链接,而不是应用程序的UIWebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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