Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSURLAuthenticationChallenge.sender返回nil_Ios_Objective C_Authentication_Basic Authentication_Nsurlsession - Fatal编程技术网

Ios NSURLAuthenticationChallenge.sender返回nil

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

请原谅我问题中的点符号

我正在使用NSURLSession尝试登录网站。苹果的文档建议以这种方式处理身份验证挑战

- (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};

谢谢你的回复。我最终选择了一个完全不同的方向。我想负责任的做法应该是双倍返回并记录我的反应。这个周末我会尽我最大的努力去做。