I am embedding a GoogleMap in my app.
On the embedded map (which is an iFrame) I have links from google:'term of use'.
When I click on the link (a customer could do that by mistake or not) it opens the page in my app and full screen. Impossible then to get back to the app!
So I added the Cordova Plugin InAppBrowser. And I created a little script to prevent that:
$("a").click(function(e) {
console.log("checking link");
var link = e.currentTarget.href;
var testURL = (link.indexOf("www") > (-1));
testURL |= (link.indexOf("http") > (-1));
testURL |= (link.indexOf(".com") > (-1));
testURL |= (link.indexOf(".fr") > (-1));
if (testURL) {/*if URL is external open in 'new window'*/
console.log("Prevent InApp URL FullScreen");
window.open(link, "_system");
}
});
And it's working very well on a classic link on the page.
The main issue is that google Map is in an iFrame! This way I can't access to elements from jQuery and the $("a').click(.....) is not working on the iFrame Link !
To sum Up
I want that any external link (http, wwww, .fr, .com, .org...etc) to open in Safari instead of my app. Or using the InAppBrowser plugin, to open in my app but not full screen, to get back to the App.
Have you any Idea?
Thanks in advance guys.
I have found a solution, this is maybe not the best one as it's only for iOs (certainly scalable to android):
I added this code in the MainViewController.m:
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType) navigationType
{ NSURL *url = [request URL];
//mustNotReroot : for the google iFrame, the adress contains "embed" ==> must not be open in safari but in the app
BOOL mustNotReroot = [url.path rangeOfString:@"embed"].location != NSNotFound;
// Intercept any external http requests and forward to Safari
// Otherwise forward to the PhoneGap WebView
if (([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"])&& !mustNotReroot)
{
NSLog(@"Rerouting to Safari %@",url);
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
{
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
This way any http or https will be forwarded to Safari.app. Pay attention because the iFrame is understood as an external link (as it is XSS iFrame) and so it was opened in Safari, so I did the fix by adding the check mustNotReroot.
The iFrame URL is : https://www.google.com/maps/embed/v1/directions?key=MyAPI_Key&origin=Campbell&destination=Sunnyvale
As it contains embed I check if it contains embed, if yes: do not forward to Safari.
If you have a better fix like a javascript fix working on both iOS and Android feel free to share it !