iOS开发笔记-47: AFNetworking3.0以后 HTTPS设置

iOS 11情况下AFNetworking 3.0如果不配置AFSecurityPolicy是工作不了的。
(1)从服务器端下载.crt文件,比如文件名叫 serverSecurity.crt
  (2)   将文件转成.cer文件.终端命令:openssl x509 -in serverSecurity.crt -out https.cer -outform der 注意:https是一个文件名,可以换成别的名字
  如果是pem的证书:
        x509 -inform PEM -outform DER -in certs/terminal.cer -out certs/terminal.cer

    (3)导入.cer文件到项目 。导入的时候注意选中target(z h如果没有选中,就找不到文件的路径)
     (4) 配置ACNetWorkManager的securityPolicy属性,贴代码:
NSString *cerPath = [[NSBundlemainBundle] pathForResource:@"https"ofType:@"cer"];
        NSData *certData  = [NSDatadataWithContentsOfFile:cerPath];
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicypolicyWithPinningMode:AFSSLPinningModeCertificate];
        securityPolicy.allowInvalidCertificates =YES;
        securityPolicy.pinnedCertificates = [NSSetsetWithObject:certData];
        securityPolicy.validatesDomainName =YES;
        manager.securityPolicy = securityPolicy;

参考的资料:
http://blog.csdn.net/u012989180/article/details/73901416

你可能感兴趣的:(iOS开发笔记-47: AFNetworking3.0以后 HTTPS设置)