validate self signed certificate wit AFNetworking

3.1k Views Asked by At

I want to connect my iOS App with AFNetwork to my webserver with a self-signed certificate. I found a solution on github (https://github.com/AFNetworking/AFNetworking/pull/694) I tried it out and the certificate pinning seems to work but i got an other error:

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7bc2090 {NSErrorFailingURLKey=(my domain)}

Does anybody know whether this error has to do with the AFNetworking Framework and the self signed certificate or not?

Solved: I found the solution for the error. I had to set the SSLPinningMode to AFSSLPinningModeCertificate now it works.

AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    NSLog(@"%@",error);
}];
operation.SSLPinningMode = AFSSLPinningModeCertificate;
[operation start];
1

There are 1 best solutions below

4
On

Try this work around.

AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSDictionary *resDictionary = (NSDictionary *)JSON;
}
                                                                                   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                       NSLog(@"%@",error);
                                                                                   }];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];