//此方法每次进行网络请求的时候,都会调用
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *urlString = [[request URL] absoluteString];
#if DEBUG
NSLog(@"==> %@",urlString);
#endif
NSMutableURLRequest *mutableRequest = [request mutableCopy];
NSDictionary *requestHeaders = request.allHTTPHeaderFields;
// 判断请求头是否已包含,如果不判断该字段会导致webview加载时死循环
if (requestHeaders[@"usertoken"]) {
return YES;
} else {
[mutableRequest setValue:value forHTTPHeaderField:@"usertoken"];
request = [mutableRequest copy];
[webView loadRequest:request];
return NO;
}
return YES;
}
网友评论