美文网首页
iOS UIWebView 增加请求头

iOS UIWebView 增加请求头

作者: CocoaH | 来源:发表于2019-10-10 14:12 被阅读0次

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

- (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 增加请求头

      本文链接:https://www.haomeiwen.com/subject/scaopctx.html