最近在配置https,使用的是AFNetWorking3.0。
一直报个NSArray object nil 闪退,开始找了项目本身的问题,一直找不到。
后来发现,是AFNetWorking里AFSecurityPolicy.m文件中的方法
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust forDomain:(NSString *)domain,代码片段里
case AFSSLPinningModeCertificate: {
NSMutableArray *Certificates = [NSMutableArray array];
for (NSData *certificateData in self.pinnedCertificates) {
[Certificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
}
SecTrustSetAnchorCertificates(serverTrust, (__bridge CFArrayRef)Certificates);
if (!AFServerTrustIsValid(serverTrust)) {
return NO;
}
// obtain the chain after being validated, which *should* contain the pinned certificate in the last position (if it's the Root CA)
NSArray *serverCertificates = AFCertificateTrustChainForServerTrust(serverTrust);
for (NSData *trustChainCertificate in [serverCertificates reverseObjectEnumerator]) {
if ([self.pinnedCertificates containsObject:trustChainCertificate]) {
return YES;
}
}
return NO;
}
这句代码获取证书的时候获取不到,数组添加了nil object出现了崩溃
[Certificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)];
解决方法:
1.将后端发过来的.crt证书,修改后缀.cer,导入钥匙串
2.再从钥匙串导出该证书,拉到项目里直接使用