AFNetworking中使用HTTPS的应用

这里介绍在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;
}


----------

    原文作者:anyway
    原文地址: https://segmentfault.com/a/1190000004702514
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞