iOS UIWebView 增加请求头

//此方法每次进行网络请求的时候,都会调用

- (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;
}

你可能感兴趣的:(iOS UIWebView 增加请求头)