这里介绍在AFNetworking中要使用https请求,导入证书的做法:
1.文件导入:
项目中需要导入后缀为.cer的证书,该证书由后台服务器根据HTTPS的要求生成
2.AFNetworking中的设置:
/**** SSL Pinning ****/
[_manager setSecurityPolicy:[Public customSecurityPolicy]];
/**** SSL Pinning ****/
3.customSecurityPolicy方法:
+ (AFSecurityPolicy *)customSecurityPolicy
{
/**** SSL Pinning ****/
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"communityFinancial" ofType:@"der"];
if (cerPath == nil) {
NS_APPDebugLog(@"***文件路径没找到");
}
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
// 自签名证书设为YES 生产X.509证书设置为NO
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setPinnedCertificates:@[certData]];
/**** SSL Pinning ****/
return securityPolicy;
}
----------