Heim > Fragen und Antworten > Hauptteil
描述你的问题
AFNetworking 无线访问正常 蜂窝数据访问出错 报 -1012
贴上相关代码
贴上报错信息
Error Domain=NSURLErrorDomain Code=-1012 "未能完成操作。(“NSURLErrorDomain”错误 -1012。)" UserInfo=0x175d6c00
贴上相关截图
已经尝试过哪些方法仍然没解决(附上相关链接)
習慣沉默2017-05-02 09:23:36
这和AFNetworking没有关系,code=-1012 是系统提供 NSURLError 的错误信息。这有对照表http://blog.csdn.net/yesjava/article/details/8056681。
NSURLErrorUserCancelledAuthentication = -1012。用户取消验证。
搜搜就知道原因了。
高洛峰2017-05-02 09:23:36
1.无线访问和蜂窝访问的DNS服务器不一样. 看报错信息,你访问的服务器开启了SSL,使用了自生成的证书.
2.AFNetworking 2.x 我没有遇到过类似问题,在3.x版本下:
2.1 代码修改:
//使用默认的security policy, 注释掉类似代码:
//sessionMgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
//以下和AFNetworking 2.x相同
sessionMgr.securityPolicy.allowInvalidCertificates = YES;
//以下是AFNetworking 3.x 下需要的
sessionMgr.securityPolicy.validatesDomainName = NO;
2.2 Info.plist 加入ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
3.代码举例:
//#自生成证书
-(IBAction)clickOnSSLWithJSON:(id)sender {
NSLog(@"ssl with json");
NSURL *url = [NSURL URLWithString:@"https://192.168.1.6:3000"];
AFHTTPSessionManager *sessionMgr=[[AFHTTPSessionManager alloc]initWithBaseURL:url];
sessionMgr.responseSerializer = [AFHTTPResponseSerializer serializer];
sessionMgr.responseSerializer.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"text/html",nil];
//in this case, Info.plist should be added with keys 'NSAppTransportPolicy' and 'NSAllowsArbitraryLoads',
//and security policy should be used with default.
//following setting is same as AFNetworking 2.x
sessionMgr.securityPolicy.allowInvalidCertificates = YES;
//following setting is specified for AFNetworking 3.x
sessionMgr.securityPolicy.validatesDomainName = NO;
[sessionMgr GET:@"" parameters:nil progress:nil
success: ^(NSURLSessionDataTask * _Nonnull task, id _Nullable result){
//NSUTF8StringEncoding should be encoded for response data.
NSLog(@"ok:\n%@",[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding]);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"error:\n%@",error);
}];
}