自升级到 iOS 7.1 后,我的应用程序出现了一些严重问题。 WebView 和身份验证质询工作正常。由于我已升级,webview 不再显示内容。身份验证似乎工作正常,因为我通过调试 + NSLog 获得了正确的值,因此无效的凭据不是问题(检查了大约 10 次)。在 webview 使用有效凭据重新加载页面后,仍然没有显示任何内容。这是我的代码:
- (void)viewDidLoad
{
_authed = NO;
[self.webView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"xxx"]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);
if (!_authed) {
_authed = NO;
[[NSURLConnection alloc] initWithRequest:request delegate:self];
return NO;
}
return YES;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] > 0) {
}
else
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"xxx"
password:@"xxx"
persistence:NSURLCredentialPersistencePermanent];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
NSLog(@"got auth challange");
if ([challenge previousFailureCount] == 0) {
_authed = YES;
[[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"xxx" password:@"xxx" persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
}
else
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
NSLog(@"received response via nsurlconnection");
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"xxx"]];
[self.webView loadRequest:urlRequest];
_authed = YES;
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
return NO;
}
NSLog输出:
2014-04-17 12:23:20.418 Test[510:60b] Did start loading: xxx auth:0
2014-04-17 12:23:20.704 Test[510:60b] received response via nsurlconnection
2014-04-17 12:23:20.710 Test[510:60b] Did start loading: xxx auth:1
提前致谢。
最佳答案
您可以在 url 中而不是请求 header 中传递身份验证请尝试代替 BASE_URL: NSString* urlString = [NSString stringWithFormat:@"%@//%@:%@@%@",@"http:",userName,password,myUrl]
关于ios - UIWebView 身份验证在 iOS 7.1 中不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130780/