user3544954
user3544954

Reputation: 1

AFNetworking can not access setAuthenticationChallengeBlock

I have installed

pod 'AFNetworking'

And added to header

#import "AFNetworking.h"
#import "AFURLConnectionOperation.h"

But I got error

No visible @interface for 'AFURLConnectionOperation' declares the selector setAuthenticationChallengeBlock

Code used

NSURLRequest *myRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"someURL"]];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];

    [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge)
    {
        if (challenge.previousFailureCount == 0)
        {
            NSURLCredential *creds = [NSURLCredential credentialWithUser:@"USERNAME"
                                                                password:@"PASSWORD"
                                                             persistence:NSURLCredentialPersistenceForSession];

            [[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
        }
        else
        {
            NSLog(@"Previous auth challenge failed. Are username and password correct?");
        }
    }];

Upvotes: 0

Views: 793

Answers (1)

Rob
Rob

Reputation: 437532

I believe you meant to use setWillSendRequestForAuthenticationChallengeBlock:

[operation setWillSendRequestForAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
    if (challenge.previousFailureCount == 0) {
        NSURLCredential *creds = [NSURLCredential credentialWithUser:@"USERNAME"
                                                            password:@"PASSWORD"
                                                         persistence:NSURLCredentialPersistenceForSession];

        [[challenge sender] useCredential:creds forAuthenticationChallenge:challenge];
    } else {
        NSLog(@"Previous auth challenge failed. Are username and password correct?");
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}];

Also, make sure that all paths of execution call one of the NSURLAuthenticationChallengeSender methods (e.g., above, I call cancelAuthenticationChallenge: if the authentication failed).

Upvotes: 1

Related Questions