Search code examples
ioscordovaphonegap-pluginschildbrowser

Make external links in ChildBrowser open with safari?


I am currently using ChildBrowser in phoneapp to open a website. However, I would like to open all external links of this website to open in Safari.

I do not have control over the source of this website.

My understanding is that I have to modify ChildBrowser to open all links starting with "http" in Safari.

I can't exactly read Objective-C, but I believe the code below is relevant.

- (void)loadURL:(NSString*)url
{
    NSLog(@"Opening Url : %@",url);

    if( [url hasSuffix:@".png" ]  || 
        [url hasSuffix:@".jpg" ]  || 
        [url hasSuffix:@".jpeg" ] || 
        [url hasSuffix:@".bmp" ]  || 
        [url hasSuffix:@".gif" ]  )
    {
        [ imageURL release ];
        imageURL = [url copy];
        isImage = YES;
        NSString* htmlText = @"<html><body style='background-color:#333;margin:0px;padding:0px;'><img style='min-height:200px;margin:0px;padding:0px;width:100%;height:auto;' alt='' src='IMGSRC'/></body></html>";
        htmlText = [ htmlText stringByReplacingOccurrencesOfString:@"IMGSRC" withString:url ];

        [webView loadHTMLString:htmlText baseURL:[NSURL URLWithString:@""]];

    }
    else if ( [url hasPrefix:@"http" ])
    {
        //I have added in this else if.
    }
    else
    {
        imageURL = @"";
        isImage = NO;
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        [webView loadRequest:request];
    }
    webView.hidden = NO;
}

Any advice?


Solution

  • The easiest way I've found to do this is, if you know the Domain Host name that you will initially be requesting with childBrowser and you don't plan on requesting any other Domains then you can write this event right into the ChildBrowserViewController.m file.

    - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request     navigationType:(UIWebViewNavigationType)navigationType
    {
        NSURL *url = [request URL];
        NSString* domain = [url host];
    
        // Intercept the external http requests and forward to Safari.app
        // Otherwise forward to the PhoneGap WebView
        if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"] || [[url scheme] isEqualToString:@"www"]) {
            if ( [domain isEqualToString:@"YOURDOMAIN.NAME] ) {
                return YES;
            } else {
                [[UIApplication sharedApplication] openURL:url];
                return NO;
             }
        } else {
             return YES;// [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
        }
    }
    

    This will catch all requests made by childBrowser and if they match standard URL convention (preceding with HTTP, HTTPS, or WWW) and they are not your Domain Host name that you preset then it will foce them to open in Safari. Then when the user returns to your app they will still be on the previous page they where when they clicked the link.

    I messed with this for hours until I came up with this.

    If you need to be able to open more than one Domain Host with childBrowser but still want each one to open all external links leading away from that current domain in Safari I would suggest the following.

    Add the following to ChildBrowserViewController.h

    BOOL scaleEnabled;
    BOOL isImage;
    NSString* originalURL; <- THIS RIGHT HERE
    NSString* imageURL;
    

    And this

    @property(retain) NSString* imageURL;
    @property(retain) NSString* originalURL; <- THIS RIGHT HERE
    @property(assign) BOOL isImage;
    

    Then add the following to ChildBrowserViewController.m

    @synthesize imageURL;
    @originalURL; <- THIS RIGHT HERE
    @synthesize supportedOrientations;
    @synthesize isImage;
    

    And add the following

    - (void)loadURL:(NSString*)url
    {
        originalURL = url; <- THIS RIGHT HERE
        NSLog(@"Opening Url : %@",url);
    

    And then in the method I posted above change the following

        NSURL *url = [request URL];
        NSString* domain = [url host];
    
        NSURL *oURL = [NSURL URLWithString: originalURL]; <- THIS RIGHT HERE
        NSString* oDomain = [oURL host]; <- THIS RIGHT HERE
    
        // Intercept the external http requests and forward to Safari.app
        // Otherwise forward to the PhoneGap WebView
        if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"] || [[url scheme] isEqualToString:@"www"]) {
            if ( [domain isEqualToString:oDomain] ) { <- THIS RIGHT HERE
                return YES;
            } else {
    

    I haven't tested this second part but it should work just fine.

    Good luck.