AFNetworking 2.0中的setTaskDidReceiveAuthenticationChallengeBlock [英] setTaskDidReceiveAuthenticationChallengeBlock in AFNetworking 2.0

查看:129
本文介绍了AFNetworking 2.0中的setTaskDidReceiveAuthenticationChallengeBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在AFNetworking 2.0版中处理身份验证吗?我尝试了以下但没有成功。该块被调用(我在那里有一个NSLog),但是响应仍然是401错误

Anybody know how to handle authentication in the 2.0 version of AFNetworking? I tried the below without success. The block is getting called (I had an NSLog in there) but the response is still a 401 error

[self setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession *session, NSURLSessionTask *task, NSURLAuthenticationChallenge *challenge, NSURLCredential *__autoreleasing *credential) {
    *credential = [[NSURLCredential alloc] initWithUser:username password:password persistence:NSURLCredentialPersistenceForSession];
    return NSURLSessionAuthChallengeUseCredential;
}];


推荐答案

我认为这是AFNetworking 2.0中的错误。这是实现:

I think this is a bug in AFNetworking 2.0. Here is the implementation:

- (void)URLSession:(NSURLSession *)session
              task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
 completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
    NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    __block NSURLCredential *credential = nil;

    if (self.taskDidReceiveAuthenticationChallenge) {
        disposition = self.taskDidReceiveAuthenticationChallenge(session, task, challenge, &credential);
    } else {
        [self URLSession:session didReceiveChallenge:challenge completionHandler:completionHandler];
        return;
    }

    if (completionHandler) {
        completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, credential);
    }
}

请注意,即使您指定了 NSURLSessionAuthChallengeUseCredential ,AFNetworking传回 NSURLSessionAuthChallengePerformDefaultHandling ,其中指出挑战的默认处理-好像未实现此委托;凭据参数将被忽略。

Note that even though you're specifying NSURLSessionAuthChallengeUseCredential, AFNetworking is passing back NSURLSessionAuthChallengePerformDefaultHandling, which states "Default handling for the challenge - as if this delegate were not implemented; the credential parameter is ignored."

这篇关于AFNetworking 2.0中的setTaskDidReceiveAuthenticationChallengeBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆