NSURLConnection的didCancelAuthenticationChallenge委托方法从未调用
问题描述:
为什么NSURLConnection的didCancelAuthenticationChallenge委托方法永远不会被调用,即使在手动取消Auth挑战(实际上会按照假设取消)之后呢?NSURLConnection的didCancelAuthenticationChallenge委托方法从未调用
我粘贴下面的相关代码的一些位,记住,像预想的那样,除了- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
感谢所有帮助被称为所有其他委托方法。 //迭戈
...
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 ?
}
答
我相信提供用于连接是否取消认证,不是你的委托方法。例如如果您花了太长时间来应对挑战,则连接理论上可能会取消身份验证。
是的,Mike,有些人似乎同意你的观点,还有一些像我这样的人认为它是一个bug,文档甚至多次自相矛盾,所以我发布了一个bug报告给苹果,让我们看看他们说了什么。谢谢! – DiegoMax 2011-02-04 14:09:32