How to assign a .cer file in AFNetworking?

2019-03-29 06:12发布

问题:

I am using AFNetworking to develop iPhone application that establish HTTPS connections ,I want use self-signed cert or trusted cert I can define ,but I cannot find any function(API) in AFNetworking can do this.

And I saw this page : How to use NSURLConnection to connect with SSL for an untrusted cert? .But I want to know how to assign a cer file?

Thanks.

回答1:

Well, just to be simple, follow this steps

  1. Copy your .cer file to your project bundle

  2. Assuming you are using

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] init...

    Invoke the following after that line:

    [operation setSSLPinningMode:AFSSLPinningModeCertificate]

This will instruct AF to do Certificate Pinning, no need to set anything else.



回答2:

Just copy cer file in your project bundle. In AFURLConnectionOperation.m file, line 215:

NSArray *paths = [bundle pathsForResourcesOfType:@"cer" inDirectory:@"."];

It will assign cer file automatically.

And make sure that Prefix.pch has follows:

#define _AFNETWORKING_PIN_SSL_CERTIFICATES_

and

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_

if untrusted cert.