cocoa - NSURLConnection 的 didCancelAuthenticationChallenge 委托(delegate)方法从未被调用

标签 cocoa delegates protocols nsurlconnection

为什么 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/

相关文章:

cocoa - cocoa中目录的AES加密

ios - UILongPressGestureRecognizer 未调用其目标方法

c - 使用强制转换来访问像结构一样的字节数组?

c - 奇怪的 Linux 套接字协议(protocol)行为

ios - id <SampleProtocolDelegate> 委托(delegate)与 id <SampleProtocolDelegate> _delegate

cocoa - NSURL URLWithString : is null with non-english accented characters

objective-c - AppleScript 编辑器中显示的 AppleScript 类名

cocoa - 再次单击应用程序图标时显示 Cocoa 应用程序的首选项

c# - 递归调用 - Action lambda

c# - 使用将委托(delegate)返回给授权调用者的方法。如何?