Skip to content

Instantly share code, notes, and snippets.

@alunny
Created December 20, 2011 22:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alunny/1503552 to your computer and use it in GitHub Desktop.
Save alunny/1503552 to your computer and use it in GitHub Desktop.
Override shouldStartLoadWithRequest
/**
* Overriding in __App__Delegate.m
* if this is viable, find a nicer API and merge into PhoneGap/Callback/Cordova edge
*
* programmatic iframe creations open in main webview
* user interactions open in Safari
*/
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL superValue = [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
BOOL inIframe = ![self doesRequest:request comeFromWebView:theWebView];
BOOL fromUserInteraction = (navigationType == UIWebViewNavigationTypeLinkClicked || navigationType == UIWebViewNavigationTypeFormSubmitted);
NSURL *url = [request URL];
NSLog(@"url is %@", url);
NSLog(@"inIframe is %@", (inIframe ? @"true" : @"false"));
NSLog(@"request.mainDocumentURL = %@", [request.mainDocumentURL absoluteString]);
NSLog(@"webview.request.mainDocumentURL = %@", [theWebView.request.mainDocumentURL absoluteString]);
// if in iframe and has user interaction
if (inIframe && fromUserInteraction) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
// otherwise, fall back to super
return superValue;
}
- (BOOL)doesRequest:(NSURLRequest *)request comeFromWebView:(UIWebView *)webView
{
// sees if the mainDocumentURL of the request matches that of the webView
// if not, is probably from an iFrame
return ([[request.mainDocumentURL absoluteString]
compare:[webView.request.mainDocumentURL absoluteString]]
== 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment