/** | |
* 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 ]; | |
} | |
} |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mineshaftgap
commented
Oct 25, 2011
Thanks for the tip! Seems to me that the else enclosure is not needed since the if has a return in it. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
mwbrooks
Oct 25, 2011
Thanks.
You're certainly right, the else
isn't required, but it does make the logic a little more explicit (to me). I'm not much of an objective-c developer, so I'm not sure what the expected convention would be.
Thanks. You're certainly right, the |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
stritti
Dec 12, 2011
Sorry I have no Idea of Objective-C, but is it possible to verify if the url is in the whitelist? I would like to open all not whitelisted URLs in external Safari
stritti
commented
Dec 12, 2011
Sorry I have no Idea of Objective-C, but is it possible to verify if the url is in the whitelist? I would like to open all not whitelisted URLs in external Safari |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
GopinathMR
Apr 4, 2012
You can add URL schemes "mailto" and "tel" as well. That way , if you have < a href=" mailto:a@b.com" > or < a href="tel:1234" >call me</ a>, they open email client and telephone app respectively.
GopinathMR
commented
Apr 4, 2012
You can add URL schemes "mailto" and "tel" as well. That way , if you have < a href=" mailto:a@b.com" > or < a href="tel:1234" >call me</ a>, they open email client and telephone app respectively. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Ruddiger
Apr 25, 2012
One issue with this fix is that iframes will also open in external Safari. Here is a solution (works with 1.4.1):
/**
-
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];if(navigationType == UIWebViewNavigationTypeLinkClicked && ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"])){
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [self.viewController webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
}
Ruddiger
commented
Apr 25, 2012
One issue with this fix is that iframes will also open in external Safari. Here is a solution (works with 1.4.1): /**
|
Nice, thanks @Ruddiger! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
JonathanSchndr
Apr 4, 2014
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 thenavigationType
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 ];
}
}
JonathanSchndr
commented
Apr 4, 2014
update - with this code you can create a iframe with inside and external links.
|
Thanks for the tip!
Seems to me that the else enclosure is not needed since the if has a return in it.