I want to be able to send some extra headers with my UIWebView loadRequest method.
I have tried:
NSMutableURLRequest *req = [NSMutableUR
I find another way, Can use NSURLProtocol .
-(BOOL)webView:(IMYVKWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSMutableDictionary* mapObject = [NSMutableDictionary dictionary];
mapObject[@"headers"] = request.allHTTPHeaderFields;
mapObject[@"navigationType"] = @(navigationType);
[webViewRequestMap setObject:mapObject forKey:request.URL.absoluteString];
return YES;
}
webViewRequestMap is Static NSMutableDictionary*
in Your Custom NSURLProtocol code:
@interface IMYCustomURLProtocol : NSURLProtocol
@end
@implementation IMYCustomURLProtocol
+(void)load
{
[NSURLProtocol registerClass:self];
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSString* urlString = request.URL.absoluteString;
NSDictionary* dictionary = webViewReuqestMap[urlString];
if (dictionary)
{
[webViewRequestMap removeObjectForKey:urlString];
if ([request isKindOfClass:[NSMutableURLRequest class]]) {
[(id)request setValue:@"HAHA" forHTTPHeaderField:@"MeiYou Co.,Ltd"];
}
}
return NO;
}
@end