public
Created

Override shouldStartLoadWithRequest

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
* 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);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.