How can I open an external link in Safari not the app's UIWebView?

后端 未结 10 1392
甜味超标
甜味超标 2020-11-28 10:47

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 saf

10条回答
  •  时光取名叫无心
    2020-11-28 10:55

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

提交回复
热议问题