2013-02-27 55 views
0

我正在爲UIWebView身份驗證創建NSURL連接。它工作正常,但由於某種原因,我無法獲得授權。如果我輸入了錯誤的密碼,它不會讓我用正確的密碼再次嘗試登錄。終止NSURLconnection的最佳方法是什麼?終止NSURLConnection ios

我嘗試這樣做:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSString *errorText = [[error userInfo] description]; 
    NSLog (@"----------------------------------------------"); 
    NSLog (@"STEP 4 - connection didFailWithError"); 
    NSLog (@"Error message: %@", errorText); 
    connection = nil; 
} 

但它不工作的某些原因。

+0

您是否嘗試在標記連接之前進行取消API調用nil?看到這個http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html – Saurabh 2013-02-27 11:46:28

+0

設置指針爲零的指針在這裏沒有效果 – Felix 2013-02-27 11:49:25

回答

0

沒有必要以你想要的方式終止NSURLConnection。

實施了以下NSURLConnection的委託方法就可以更改憑證(登錄/密碼):

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 

在這種方法中,你有一個變化提供了新的憑據。類似的東西:

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    NSLog(@"received authentication challenge"); 
    NSURLCredential *newCredential = [NSURLCredential credentialWithUser:@"USER" 
                   password:@"PASSWORD" 
                  persistence:NSURLCredentialPersistenceForSession]; 
    NSLog(@"credential created"); 
    [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    NSLog(@"responded to authentication challenge");  
}