Skip to content

Instantly share code, notes, and snippets.

@naveen
Last active December 16, 2015 15:19
Show Gist options
  • Select an option

  • Save naveen/5455458 to your computer and use it in GitHub Desktop.

Select an option

Save naveen/5455458 to your computer and use it in GitHub Desktop.
intercept custom JS handlers and inject custom HTTP headers in a UIWebView
- (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