didReceiveAuthenticationChallenge 导致 EXC_BAD_ACCESS
如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“已创建凭据”后会立即出现 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];
}
}
If the website doesn't require authentication, my code works fine, if it does, it errors with EXC_BAD_ACCESS right after printing "credential created". I'm not releasing anything, and this code is copied straight from the documentation - any idea what's wrong?
- (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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,这与特定网站的身份验证方法有关 - 将我想要登录的网站替换为另一个网站,代码工作正常。
(我试图从 unRaid 的状态页面抓取数据,unRaid 不使用实际的网络服务器,而是使用 emhttp,所以我假设这与它有关)
Turns out it's something to do with that particular sites authentication method - replacing the website I want to log into with another website and the code works fine.
(I was trying to scrape data from unRaid's status page, unRaid doesn't use an actual webserver but emhttp so I'm assuming that has something to do with it)
您唯一无法控制的是
[challenge sender]
的返回值。尝试打印其描述以确保它非零。The only thing you're not controlling is the return value of
[challenge sender]
. Try printing its description to ensure that it's non-nil.