iOS 11.1.1 af网络请求失败的解决办法
升级iOS 11.1.1之后出现网络请求错误
Error Domain=NSURLErrorDomain Code=-1004 "未能连接到服务器。" UserInfo={NSUnderlyingError=0x1c4a53dd0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://www.cheshizh.com/?m=app&c=app_mall&a=app_focus&city=%E9%95%BF%E6%98%A51, NSErrorFailingURLKey=http://www.cheshizh.com/?m=app&c=app_mall&a=app_focus&city=%E9%95%BF%E6%98%A51, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=未能连接到服务器。}
iOS 11的ATS相关问题。
iOS 11情况下AFNetworking 3.0如果不配置AFSecurityPolicy是工作不了的。
(1)从服务器端下载.crt文件,比如文件名叫 serverSecurity.crt
(2) 将文件转成.cer文件.终端命令:openssl x509 -in serverSecurity.crt -out https.cer -outform der 注意:https是一个文件名,可以换成别的名字
(3)导入.cer文件到项目 。导入的时候注意选中target
(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;
总结:(1)配置过程不难
(2)Apple不再向开发者妥协
(3)iOS应用越来越安全
AFHTTPSessionManager * session = [AFHTTPSessionManager manager];
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@“mnchip”ofType:@“cer”];
NSData * cerDat = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy * SECURITYPOLICY = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[NSSet中setWithObject:cerDat]];
//允许自检证书
securityPolicy.allowInvalidCertificates = YES;
//域名与服务器一致
securityPolicy.validatesDomainName = YES;
session.securityPolicy = [OperationHelper customSecurityPolicy];
的NSDictionary * PARAMS = [NSDictionary的dictionaryWithObjectsAndKeys:@ “DoPassword”,VALUE_NAME,temDevicenum,VALUE_ID,@(时间),VALUE_TIMESTAMP,MMM,VALUE_MD5,@ “” @ “cPassword”,零]。
[session POST:KURL参数:params进度:nil成功:^(NSURLSessionDataTask * _Nonnull任务,id _Nullable响应对象){
NSDictionary * dictionary =(NSDictionary *)responseObject;
if([dictionary [@“ReturnValue”] intValue] == 1){
// NSLog(@“请求登陆口令成功”);
}
}失败:^(NSURLSessionDataTask * _Nullable任务,NSError * _Nonnull错误){
// NSLog(@