为什么 NSURLConnection 的 didCancelAuthenticationChallenge 委托(delegate)方法永远不会被调用,即使在手动取消身份验证质询之后(实际上按预期被取消)?
我在下面粘贴了一些相关代码,请记住,除了 - (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 之外,所有其他委托(delegate)方法均按照假定进行调用
感谢您的帮助。 //迭戈
...
NSURLConnection *serviceConnection = [NSURLConnection connectionWithRequest:serviceRequest delegate:self];
...
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
if ([challenge previousFailureCount]) {
NSLog(@"Invalid credentials... Cancelling.");
[[challenge sender] cancelAuthenticationChallenge:challenge];
// AT THIS POINT cancelAuthenticationChallenge HAS BEEN CALLED, YET, DELEGATE METHOD IS NOT CALLED.
} else {
if ([ud stringForKey:@"username"] && [ud stringForKey:@"password"]) {
NSLog(@"Service is trying to login with locally stored user and password from NSUserDefaults");
NSURLCredential *credential = [NSURLCredential credentialWithUser:[ud stringForKey:@"username"]
password:[ud stringForKey:@"password"]
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender]useCredential:credential forAuthenticationChallenge:challenge];
} else {
[delegate STServiceNeedsLoginInfo:self];
}
}
}
- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"Failed login with status code: %d", [(NSHTTPURLResponse*)[challenge failureResponse]statusCode]);
// THIS METHOD IS NEVER CALLED. WHY ?
}
最佳答案
我相信如果连接取消身份验证,则提供委托(delegate)方法,而不是您。例如如果您响应挑战的时间过长,理论上连接可能会取消身份验证。
关于cocoa - NSURLConnection 的 didCancelAuthenticationChallenge 委托(delegate)方法从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4882725/