David
David

Reputation: 734

Phonegap - Open Link in Mobile Safari

Before I updated my application to Cordova 2.5.0, my basic link opened up into Safari. Now it just loads in my Application view and not into Safari.

I had the following code: Contact Us by visiting, <a href="http://site.com/contact.php"> our website! </a>

Any idea on how I can fix this.

For more information: it all started when we switched to config.xml from Cordova/Phonegap.plist.

Upvotes: 1

Views: 6001

Answers (4)

Brandon J Brotsky
Brandon J Brotsky

Reputation: 523

  1. Add target="_blank" to your links. ie:

    <a href="http://www.brandonbrotsky.com/" target="_blank"></a>
    
  2. Make sure access has an origin of * /> in your config.xml (make sure its the one in the root of the app directory, above the www folder. ie:

    <access origin="*" />
    
  3. Add the following code to MainViewController.m

    - (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:@"http"] || [[url scheme] isEqualToString:@"https"]) {
            [[UIApplication sharedApplication] openURL:url];
            return NO;
        }
        else {
            return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
        }
    } 
    

I made a quick video explaining how to fix this issue:

http://www.youtube.com/watch?v=zqbjXSnAR-Q&feature=c4-overview&list=UUefS6KLvFQVjhmL6hiBq4Sg

Hope it helps!

Upvotes: 4

siebmanb
siebmanb

Reputation: 807

You have to use this : window.open('http://google.com','_system');

Source is here : http://wiki.apache.org/cordova/InAppBrowser

Upvotes: 1

anilkumar07
anilkumar07

Reputation: 106

1)open links in safari

paste the code in your MainViewController.m file

- (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:@"http"] || [[url scheme] isEqualToString:@"https"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}

2)open links in InAppBrowser

function OpenLink()
                    {
                    alert(adlink); //adlink: http://www.google.com
                        var ref = window.open(adlink, '_blank', 'location=yes');//adlink is url
                        var myCallback = function() { alert(event.url); }
                        ref.addEventListener('loadstart', myCallback);
                        ref.removeEventListener('loadstart', myCallback);
                          }

Upvotes: 5

pech0rin
pech0rin

Reputation: 5016

I have been wrestling with this problem a lot lately and have come up with an answer that works for me at least. Cordova 2.6 and JQuery Mobile.

<a href="http://yoursite.something.com" target="_system"> the external link </a>

A fair amount of answers have you using target="_blank". But that just opens the link in their WebView. Hope this works for you like it did for me!

Upvotes: 1

Related Questions