如果网站不需要身份验证,我的代码可以正常工作,如果确实如此,则在打印“凭据创建”后立即出现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];
}
}
答案 0 :(得分:1)
原来这与特定网站身份验证方法有关 - 将我想要登录的网站替换为其他网站,代码运行正常。
(我试图从unRaid的状态页面抓取数据,unRaid不使用实际的网络服务器但是emhttp所以我假设它与它有关)
答案 1 :(得分:0)
你唯一不控制的是[challenge sender]
的返回值。尝试打印其描述以确保它是非零的。