AFNetworking 使用自建证书访问HTTPS

1.将cer证书拖到项目工程中
2.获取cerPath文件路径,读取cerData
3.使用 AFSSLPinningModeCertificate创建AFSecurityPolicy对象
4.参数 securityPolicy.allowInvalidCertificates 设置允许无效证书
5.参数 securityPolicy.validatesDomainName 验证域名
6.设置证书
7.将security对象赋值给manager 开始网络请求

      NSString * cerPath = [[NSBundle mainBundle]pathForResource:@"poc_server" ofType:@"cer"];
      NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
    
      AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
      securityPolicy.allowInvalidCertificates = YES;
      securityPolicy.validatesDomainName = YES;
    
      securityPolicy.pinnedCertificates = [NSSet setWithObject:cerData];
      manager.securityPolicy = securityPolicy;

你可能感兴趣的:(AFNetworking 使用自建证书访问HTTPS)