请原谅我问题中的点符号。
我正在使用NSURLSession尝试登录一个网站。Apple的文档建议以这种方式处理身份验证挑战。
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"<email>"
password:@"<password>"
persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:credential
forAuthenticationChallenge:challenge];
}问题是质询发送者返回nil。我是不是在设置中遗漏了什么?
发布于 2014-04-24 02:57:21
我不确定您是否弄清楚了这一点,但是您应该调用完成处理程序来代替旧的useCredential方法:
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);发布于 2014-06-17 23:42:00
您可以尝试使用http header进行会话授权:
NSString *userPasswordString = [NSString stringWithFormat:@"%@:%@", user, password];
NSData * userPasswordData = [userPasswordString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64EncodedCredential = [userPasswordData base64EncodedStringWithOptions:0];
NSString *authString = [NSString stringWithFormat:@"Basic %@", base64EncodedCredential];
configuration.HTTPAdditionalHeaders = @{@"Authorization": authString};https://stackoverflow.com/questions/22123280
复制相似问题