描述你的问题
AFNetworking 无线访问正常 蜂窝数据访问出错 报 -1012
贴上相关代码
贴上报错信息
Error Domain=NSURLErrorDomain Code=-1012 "未能完成操作。(“NSURLErrorDomain”错误 -1012。)" UserInfo=0x175d6c00
贴上相关截图
已经尝试过哪些方法仍然没解决(附上相关链接)
習慣沉默2017-05-02 09:23:36
This has nothing to do with AFNetworking, code=-1012 is the NSURLError error message provided by the system. There is a comparison table http://blog.csdn.net/yesjava/article/details/8056681.
NSURLErrorUserCancelledAuthentication = -1012. User cancels verification.
You will know the reason after searching.
高洛峰2017-05-02 09:23:36
1. The DNS servers for wireless access and cellular access are different. Looking at the error message, the server you accessed has SSL enabled and uses a self-generated certificate.
2. I have not encountered similar problems with AFNetworking 2.x. .x version:
2.1 Code modification:
//使用默认的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 Join ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
3. Code example:
//#自生成证书
-(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);
}];
}