AFNetworking 2.0 - unexpected NSURLErrorDomain error -1012

Charis picture Charis · Mar 11, 2014 · Viewed 11.4k times · Source

We ran into the following issue with our app that uses AFNetworking 2.0. When using AFHTTPRequestOperationManager's GET method, we got an error NSURLErrorDomain code -1012. The request used HTTPS and the server does not require user authentication. The request never reached the server by the way.

We have run several tests and this is the first time the error was produced and we are wondering how this error can get produced because it does not seem relevant.

Setup of AFHTTPRequestOperationManager :

httpOperationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL: 
        [NSURL URLWithString: HTTPS_URL)]];

httpOperationManager.responseSerializer = 
        [AFXMLParserResponseSerializer serializer];

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled: YES];

GET REQUEST

AFHTTPRequestOperation *op =[httpOperationManager GET: 
        [NSString stringWithFormat:SOME_PATH] 
        parameters:nil 
        success:^(AFHTTPRequestOperation *operation, id responseObject) { 
            //code to setup NSXMLParser ...
        } 
        failure: ^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error %@", [error localizedDescription]);
        }];

Answer

reinaldoluckman picture reinaldoluckman · Apr 11, 2014

I think you already solved the problem, but if you are trying to authenticate in a server that doesn't have a valid certificate you have to set YES for property allowInvalidCertificates in your AFHTTPRequestOperationManager object:

[yourManager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"your_username" password:@"your_password"];
[yourManager.securityPolicy setAllowInvalidCertificates:YES];

Also, as @a1phanumeric said, it can be necessary to include this line:

[yourManager.securityPolicy setValidatesDomainName:NO];

Cheers.