2013-02-27 51 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");  
}