iOS配置HTTPS(AFN)
原创
©著作权归作者所有:来自51CTO博客作者mb63e0703549da8的原创作品,请联系作者获取转载授权,否则将追究法律责任
费话不说,直接代码:
+(AFURLSessionManager*) getSessionManager{
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
[config setTimeoutIntervalForRequest:HTTP_CONNECTION_TIMEOUT]; //设置请求时间
[config setTimeoutIntervalForResource:HTTP_TIMEOUT]; //设置请求时间
AFURLSessionManager* manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
///***************增加https验证******************//
NSString *certFilePath = [[NSBundle mainBundle] pathForResource:@"app" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:certFilePath];
NSSet *certSet = [NSSet setWithObject:certData];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:certSet];
securityPolicy.allowInvalidCertificates = YES;//是否允许使用自签名证书
securityPolicy.validatesDomainName = NO;//是否需要验证域名,默认YES
manager.securityPolicy = securityPolicy;
///***************https验证结束******************//
return manager;
}
Info.plist中修改:
NSAllowsArbitraryLoads为true