前奏
使用openssl进行证书格式转换:
.pem -> .cer
openssl x509 -outform der -in certificate.pem -out certificate.cer
参考链接 http://blog.csdn.net/linda1000/article/details/8676330
正题
总共分三步:
第一步
将.cer拖到工程里面,注意检查是否真的包含到项目下,否则取不到(数据为空)
第二步
AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//配置https
session.securityPolicy = [self customSecurityPolicy];
session.securityPolicy.allowInvalidCertificates = YES;
#pragma mark- 配置https
- (AFSecurityPolicy *)customSecurityPolicy {
/** https */
NSString*cerPath = [[NSBundle mainBundle] pathForResource:@"22222.cer"ofType:nil];
NSData*cerData = [NSData dataWithContentsOfFile:cerPath];
NSSet*set = [[NSSet alloc] initWithObjects:cerData,nil];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:set];
return policy;
}
第三步
info.plist文件配置
如有白名单等其他配置 参考链接 https://onevcat.com/2016/06/ios-10-ats/