mhartington
mhartington

Reputation: 7025

Force iOS app opens external links in safari

I have an iOS app that I made using PhoneGap and JQuery mobile. The app has some external links that I want to open in mobile safari but as of now, they just open in the app view. The links are written like this :

<a rel="external" href="wwww.example.com">Click Here</a>

I read JQuery mobiles docs and it stated that adding rel="external" would solve this but apparently not. Any ideas? Keep in mind, this is a HTML base app.

Upvotes: 3

Views: 4026

Answers (2)

Andreas Otten
Andreas Otten

Reputation: 11

Nice helped me out a bit, but it opens the link automtically By putting:

if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
}

It worked, now when user clicks a url with http:// or https:// in it it opens in safari's

so totally i got this code:

if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if( [[url scheme] isEqualToString:@"http"] ||

   [[url scheme] isEqualToString:@"https"])

{

    [[UIApplication sharedApplication] openURL:url];

    return NO;

}

else

{

    return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];

}
}

Upvotes: 1

mhartington
mhartington

Reputation: 7025

Finally was able to do it by navigating to MainviewController.m and looking for a section where it mentioned webView as mentioned in the other posts then changing it from this

/* Comment out the block below to over-ride */

/*

- (void) webViewDidStartLoad:(UIWebView*)theWebView
{
    return [super webViewDidStartLoad:theWebView];
}

- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error
{
    return [super webView:theWebView didFailLoadWithError:error];
}

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
*/

TO this

/**

 * Start Loading Request

 * This is where most of the magic happens... We take the request(s) and process the response.

 * From here we can re direct links and other protocalls to different internal methods.

 */

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

    NSURL *url = [request URL];

    // add any other schemes you want to support, or perform additional

    // tests on the url before deciding what to do -jm

    if( [[url scheme] isEqualToString:@"http"] ||

       [[url scheme] isEqualToString:@"https"])

    {

        [[UIApplication sharedApplication] openURL:url];

        return NO;

    }

    else

    {

        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];

    }



}

Im have no experience with objective-c so I had to experiment with this so I'm glad I got it to work.

Upvotes: 9

Related Questions