如何在 shouldStartLoad 中正确创建新请求
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
BOOL shouldNavigate = NO;
NSString *existingAuthValue = [request valueForHTTPHeaderField:@"Authorization"];
if (existingAuthValue == nil)
{
NSString *authValue = [NSString stringWithFormat:@"Basic %@", AFBase64EncodedStringFromString(basicAuthCredentials)];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:request.URL];
//Append any other info from the old request
[webView performSelector:@selector(loadRequest:) withObject:newRequest afterDelay:0];
}
else
{
shouldNavigate = YES;
}
return shouldNavigate;
}
编辑:对不起,我没有考虑到 NSURLRequest != NSMutableURLRequest。我会更新代码。