NSURLConnection的didCancelAuthenticationChallenge委托方法从未调用过

时间:2011-02-03 05:11:00

标签: cocoa delegates protocols nsurlconnection

为什么NSURLConnection的didCancelAuthenticationChallenge委托方法永远不会被调用,即使在手动取消Auth质询之后(实际上它已按照假设被取消)?

我粘贴下面相关代码的一些部分,请记住,所有其他委托方法都被调用为- (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

的假设EXCEPT

感谢您的帮助。 //迭

...
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 ?
}

1 个答案:

答案 0 :(得分:2)

我认为如果连接取消身份验证,则提供委托方法,而不是您。例如如果你花了太长时间来应对挑战,理论上连接可以取消认证。