Iphone didReceiveAuthenticationChallenge导致EXC\u访问错误

Iphone didReceiveAuthenticationChallenge导致EXC\u访问错误,iphone,nsurlconnection,exc-bad-access,nsurlcredential,Iphone,Nsurlconnection,Exc Bad Access,Nsurlcredential,如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“credential created”后会出现EXC_BAD_ACCESS错误。我没有发布任何东西,而这段代码是直接从文档中复制的-知道怎么回事吗 - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge

如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“credential created”后会出现EXC_BAD_ACCESS错误。我没有发布任何东西,而这段代码是直接从文档中复制的-知道怎么回事吗

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
 if ([challenge previousFailureCount] == 0)
 {
  NSLog(@"received authentication challenge");

  NSURLCredential *newCredential;
  newCredential=[NSURLCredential credentialWithUser:@"root"
             password:@"rootpassword"
             persistence:NSURLCredentialPersistenceForSession];


  NSLog(@"credential created");

  [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];

  NSLog(@"responded to authentication challenge");

 }
 else
 {
  NSLog(@"previous authentication failure");

  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Failure" 
              message:@"Website did not accept the username and password."
                delegate:nil
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil];
  [alert show];
  [alert release];
 }
}

您唯一无法控制的是
[challenge sender]
的返回值。尝试打印它的描述,以确保它不是零。

结果表明,这与特定的站点身份验证方法有关-将我要登录的网站替换为其他网站,代码工作正常

(我试图从unRaid的状态页中获取数据,unRaid不使用实际的Web服务器,而是使用emhttp,所以我假设这与此有关)