AFNetworking 3.0 and SSL authentication with setSessionDidReceiveAuthenticationChallengeBlock

Viewed 3331

I'm attempting to migrating from AFNetworking 2.0 to 3.0.

In the previous version I would create an AFHTTPRequestOperation and handle client SSL authentication in another block with setWillSendRequestForAuthenticationChallengeBlock.

For example:

[operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
    if ([challenge previousFailureCount] > 0) {
        //this will cause an authentication failure
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        return;
    }

    //this is checking the server certificate
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        SecTrustResultType result;
        //This takes the serverTrust object and checkes it against your keychain
        SecTrustEvaluate(challenge.protectionSpace.serverTrust, &result);

Could someone show me an example of how to do this with AFHTTPSessionManager?

Do I need to make requests using AFURLSessionManager instead? I do see a block method there:

(void)setSessionDidReceiveAuthenticationChallengeBlock:(nullable NSURLSessionAuthChallengeDisposition (^)(NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * _Nullable __autoreleasing * _Nullable credential))block

1 Answers
Related