allow invalid certificates with AFNetworking

帅比萌擦擦* 提交于 2019-12-04 19:19:49

问题


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?


回答1:


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 :)




回答2:


As far as I know. It's an iOS 7 issue. Apple does not allow communication with self signed certificate websites. Unless you send the certificate to device and add as a trusted certificate list.

Supporting comment on other question: https://stackoverflow.com/a/20251011/753603

Couldn't find piece of documented text by Apple.



来源:https://stackoverflow.com/questions/20814793/allow-invalid-certificates-with-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!