Last active
December 16, 2015 15:19
-
-
Save naveen/5455458 to your computer and use it in GitHub Desktop.
intercept custom JS handlers and inject custom HTTP headers in a UIWebView
This file contains hidden or 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
| - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType | |
| { | |
| BOOL headersArePresent = [[request allHTTPHeaderFields] objectForKey:@"X-Custom-Timezone"] != nil; | |
| if (!headersArePresent) { | |
| dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
| dispatch_async(dispatch_get_main_queue(), ^{ | |
| NSURL *url = [request URL]; | |
| if ([[url scheme] isEqualToString:@"customjs"]) { | |
| // parse the rest of the URL object and execute functions | |
| if ([[url path] isEqualToString:@"/customNativeCall"]) { | |
| [self customNativeCall:nil]; | |
| } | |
| } else { | |
| NSMutableURLRequest* r = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; | |
| // set the new headers | |
| [r addValue:[[NSTimeZone systemTimeZone] name] forHTTPHeaderField:@"X-Custom-Timezone"]; | |
| [r setHTTPMethod:[request HTTPMethod]]; | |
| [r setHTTPBody:[request HTTPBody]]; | |
| // reload the request | |
| [self.web loadRequest:r]; | |
| } | |
| }); | |
| }); | |
| return NO; | |
| } | |
| return YES; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment