Created
December 20, 2011 22:21
-
-
Save alunny/1503552 to your computer and use it in GitHub Desktop.
Override shouldStartLoadWithRequest
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
/** | |
* 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