Ios NSURLAuthenticationChallenge.sender返回nil
请原谅我问题中的点符号 我正在使用NSURLSession尝试登录网站。苹果的文档建议以这种方式处理身份验证挑战Ios NSURLAuthenticationChallenge.sender返回nil,ios,objective-c,authentication,basic-authentication,nsurlsession,Ios,Objective C,Authentication,Basic Authentication,Nsurlsession,请原谅我问题中的点符号 我正在使用NSURLSession尝试登录网站。苹果的文档建议以这种方式处理身份验证挑战 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAu
- (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];
}
-(void)URLSession:(NSURLSession*)session didReceiveChallenge:(nsurAuthenticationChallenge*)challenge
completionHandler:(void(^)(NSURLSessionAuthChallengeDisposition,NSURLCredential*)completionHandler
{
NSURLCredential*credential=[NSURLCredential credentialWithUser:@]
密码:@“”
持久性:NSURLEdentialPersistenceOne];
[[challenge sender]使用凭证:凭证
验证质询:质询];
}
问题是[challenge sender]返回nil。我的设置中遗漏了什么吗?不确定您是否发现了这一点,但您应该调用完成处理程序来代替旧的useCredential方法:
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
您可以尝试使用http标头进行会话授权:
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};
谢谢你的回复。我最终选择了一个完全不同的方向。我想负责任的做法应该是双倍返回并记录我的反应。这个周末我会尽我最大的努力去做。