2011-02-03 76 views
2

爲什麼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 ? 
} 

回答

2

我相信提供用於連接是否取消認證,不是你的委託方法。例如如果您花了太長時間來應對挑戰,則連接理論上可能會取消身份驗證。

+0

是的,Mike,有些人似乎同意你的觀點,還有一些像我這樣的人認爲它是一個bug,文檔甚至多次自相矛盾,所以我發佈了一個bug報告給蘋果,讓我們看看他們說了什麼。謝謝! – DiegoMax 2011-02-04 14:09:32

相關問題