后台配置了https证书,如果直接将项目中的域名的 http 改成 https 会报服务器域名证书无效的错误提示。

如果项目中使用 AFNetworking 网络框架,适配如下:

1
2
3
4
5
6
7
8
9
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 适配HTTPS
manager.securityPolicy = [self configHttps];
[manager GET:URLString parameters:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}
1
2
3
4
5
6
7
8
9
10
11
/**
* 适配Https
*/
- (AFSecurityPolicy *)configHttps {
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//如果是需要验证自建证书,需要设置为YES
securityPolicy.allowInvalidCertificates = NO;
//validatesDomainName 是否需要验证域名,默认为YES;
securityPolicy.validatesDomainName = NO;
return securityPolicy;
}