AlexC
AlexC

Reputation: 9661

Phonegap 2.0 , Cordova external links

I have a situation and I spend so much time in google with no success.

I want to open in my app (IOS), external links which are like that

"<a href="http://google.com" target="_blank">External Link</a>" to open in safari not web view. where I have set up in "Cordova.plist"

OpenAllWhitelistURLsInWebView : true

Because I hav as well some Iframe inside my app, where I want to keep user in web view and not to leave the app.

An I have no idea why target="_blank" doesn't work, where here :

https://build.phonegap.com/blog/access-tags it says:

" on iOS, if a domain is whitelisted, a link will take over the entire webview, unless the link's target is _blank, in which case it will open in the browser. If it is not, it will log an error on the device, while doing nothing from the user's perspective. "

I tried to use JS way as well,

window.open('http://www.google.com', '_blank');

with no success :(

PS: I do have all my links in External host set up

I appreciate any help.

Thanks!

Upvotes: 2

Views: 1892

Answers (2)

What you need is this charmer in your 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 ];
}
}

Upvotes: 4

Redfox
Redfox

Reputation: 1034

It works with me using following setup:

Cordova.plist:

OpenAllWhitelistURLsInWebView: false

external Hosts: google.com

Link in Code:

< a target='_blank' href='http://maps.google.com/maps?q=something'>

Hope it works for you as well :)

Upvotes: 0

Related Questions