如果网站不需要身份验证,我的代码可以正常工作,如果需要,它会在打印“创建凭据”后立即出现 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];
}
}