且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 Safari 中打开外部链接而不是应用程序的 UIWebView?

更新时间:2022-12-21 08:47:09

如果你想在 safari 中打开的链接都包含一个公共字符串,你可以使用下一段代码.

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 ];
    }
}

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

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.

希望能帮到你

更新:

代码应该放在 MainViewControler 中,至少对于cordova 2.2.0.

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];