Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

allow invalid certificates with AFNetworking

I have been using following code

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.allowsInvalidSSLCertificate=YES;

Now i have changed AFNetworking to latest version that is 2.0. operation.allowsInvalidSSLCertificate is not working anymore with AFHTTPRequestOperation. As per documents i used

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = YES;

and my request code is

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];

[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog (@"success: %@", operation.responseString);

}
                                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                      NSLog(@"error: %@",  error.description);
                                  }
 ];

[operation start];
[operation waitUntilFinished];

But this is not working for HTTPS which require certificates. What should i do to make this work?

like image 330
Hassy Avatar asked Dec 28 '13 12:12

Hassy


1 Answers

I solved this problem by adding following code before [operation start];

AFSecurityPolicy *sec=[[AFSecurityPolicy alloc] init];
[sec setAllowInvalidCertificates:YES];
operation.securityPolicy=sec;

This happened because AFHTTPRequestOperationManager is not connected to AFHTTPRequestOperation. So setting manager's security certificate can not do magic at requestOperation. So have to initialize and assign one to AFHTTPRequestOperation.

Hope this help somebody :)

like image 141
Hassy Avatar answered Oct 18 '22 17:10

Hassy