I have a Phonegap (cordova) application where I want to load some external webpages within the phonegap WebView and I have other external webpages that I want to load in saf
If the links you want to open in safari all contain a common string, you can use the next piece of code.
- (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:@"SCHEME"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
This code placed in the AppDelegate.m
will open all URL that use the specified SCHEME in Safari.
I'm afraid that is all I could come up with.
Hope this helps
UPDATE :
The code should be placed in the MainViewControler, at least for cordova 2.2.0.
The method is initially commented. I had to use it to redirect Google maps links :
NSRange isGoogleMaps = [[url absoluteString] rangeOfString:@"maps.google.com" options:NSCaseInsensitiveSearch];
NSRange isGoogleTerms = [[url absoluteString] rangeOfString:@"terms_maps.html" options:NSCaseInsensitiveSearch];
if(isGoogleMaps.location != NSNotFound || isGoogleTerms.location != NSNotFound ) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
Just catch all links in your javascript that have target="_blank"
, and pass them to window.open with the '_system' param. This will work on both iOS and Android.
$(document).on('click', 'a[target="_blank"]', function(ev) {
var url;
ev.preventDefault();
url = $(this).attr('href');
window.open(url, '_system');
});
Add target="_blank" to your links. ie:
<a href="http://www.brandonbrotsky.com/" target="_blank"></a>
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="*" />
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!
The right way of doing this is using the inAppBrowser plugin
install it using the cordova CLI:
cordova plugin add org.apache.cordova.inappbrowser
Then, to open a link on safari just use:
window.open('http://apache.org', '_system');
There is a newer version of the plugin hosted on npm
to install it from the cordova CLI:
cordova plugin add cordova-plugin-inappbrowser
To open the website on safari you can use
cordova.InAppBrowser.open('http://apache.org', '_system');
or, if you want to continue using window.open like the older version you can just do this on device ready event:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open = cordova.InAppBrowser.open;
}
iside the xcode
//Place code in /Classes/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 ]; }
}
This is what works for me based on the answer by @TDeBailleul. Basically, any link that has a suffix of PDF, OR if it is a specific page I want to open in Safari, OR if it is not a page within www.example.com/* (an external link) it will open in a new window:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
// Open PDF files, Specific Pages, and External Links (not beginning with http://www.example.com) in Safari.app
if (
(navigationType == UIWebViewNavigationTypeLinkClicked) &&
([[[request URL] absoluteString] hasSuffix:@"pdf"] || [[[request URL] absoluteString] hasPrefix:@"http://www.example.com/specific-page.php"] || ![[[request URL] absoluteString] hasPrefix:@"http://www.example.com"])
) {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
return YES;
}
Hope this helps others!