0

我正在NSURL为身份验证创建连接UIWebView。它工作正常,但由于某种原因我无法获得授权。如果我输入了错误的密码,它不会让我再次尝试使用正确的密码登录。终止 的最佳方法是NSURLconnection什么?

我试过这个:

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

但由于某种原因它不起作用。

4

1 回答 1

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");    
}
于 2013-02-27T11:53:17.160 回答