Skip to content

Instantly share code, notes, and snippets.

@ghawkgu
Last active April 5, 2018 20:25
Show Gist options
  • Save ghawkgu/4621586 to your computer and use it in GitHub Desktop.
Save ghawkgu/4621586 to your computer and use it in GitHub Desktop.
Intercept the request of uiwebview, add customized header in the request. For detailed info, read http://www.nomadplanet.fr/2010/09/custom-http-headers-for-every-request-made-in-uiwebviews/ .
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType {
NSDictionary *headers = [aRequest allHTTPHeaderFields];
BOOL hasWhateverAddedHeader = NO;
for(NSString *key in [headers allKeys]) {
if([[key lowercaseString] isEqualToString:@"my-added-header"]) {
hasWhateverAddedHeader = YES;
break;
}
}
if(!hasWhateverAddedHeader) {
NSMutableURLRequest *newRequest = [aRequest mutableCopy];
[newRequest addValue:@"whatever" forHTTPHeaderField:@"My-Added-Header"];
[self loadRequest:newRequest];
[newRequest release];
return NO;
}
return YES;
}
@injectios
Copy link

That doesn't work.
UIWebView ignores custom headers and next request doesn't include it.

Env. iOS 7 iPhone 4S

check this out:
http://stackoverflow.com/questions/12844598/send-custom-headers-with-uiwebview-loadrequest

@uyewshrdjklnf
Copy link

Not working for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment