Created
February 15, 2011 18:43
-
-
Save mwbrooks/827979 to your computer and use it in GitHub Desktop.
ON PhoneGap-iOS, open external links in the external browser (Safari.app)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Note: the following function should already exist in your application delegate file. | |
* Replace it with the following implementation. | |
* | |
* Thanks to @purplecabbage for implementation. | |
* Thanks to Ruddiger for the iFrame patch. | |
*/ | |
// ... | |
/** | |
* 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]; | |
// Intercept the external http requests and forward to Safari.app | |
// Otherwise forward to the PhoneGap WebView | |
// Avoid opening iFrame URLs in Safari by inspecting the `navigationType` | |
if (navigationType == UIWebViewNavigationTypeLinkClicked && [[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) { | |
[[UIApplication sharedApplication] openURL:url]; | |
return NO; | |
} | |
else { | |
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
update - with this code you can create a iframe with inside and external links.
you need inside links? add a relative url -> test.php
you need external links? add a absolute url with http: or https and #external
(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
// Avoid opening iFrame URLs in Safari by inspecting the
navigationType
if (navigationType == UIWebViewNavigationTypeLinkClicked && [[url scheme] isEqualToString:@"#external"] && [[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}