How to assign a .cer file in AFNetworking?

2019-03-29 06:11发布

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.

2条回答
smile是对你的礼貌
2楼-- · 2019-03-29 06:54

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.

查看更多
何必那么认真
3楼-- · 2019-03-29 06:59

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.

查看更多
登录 后发表回答