AFNetworking iOS11 – HTTP加载失败-999
我有一个生产中的应用程序运行良好一段时间。 但是,自iOS 11以来 – 我收到此错误:
任务。 HTTP加载失败(错误代码:-999 [1:89])任务。已完成错误 – 代码:-999
这仅在iOS 11上发生。 而且,它不一致。 在对同一URL的五个请求中,有两个将失败。 在蜂窝连接上,它不常发生,但它仍然存在。
我正在使用AFNetworking并且如上所述; 这在iOS11之前从未出现过问题。
有任何想法吗?
更新以添加更多详细信息:
我正在使用SSL Pinning和有效证书。 我的info.plist看起来像这样:
NSAppTransportSecurity NSExceptionDomains url.com NSExceptionAllowsInsecureHTTPLoads NSExceptionRequiresForwardSecrecy NSIncludesSubdomains
像这样使用SSL固定:
NSString *pathToProdCert = [[NSBundle mainBundle]pathForResource:@"MY_CERT" ofType:@"cer"]; if(pathToProdCert){ manager.securityPolicy.pinnedCertificates = [NSSet setWithObjects[NSData dataWithContentsOfFile:pathToProdCert], nil]; manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]; }
更新:回应评论:这是经理的宣布方式:
@property (nonatomic) AFHTTPSessionManager *manager;
管理器通过我的Web服务类的init方法初始化:
-(instancetype)initWithBaseUrl:(NSURL *)url { self = [super init]; if (self){ self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; self.manager.responseSerializer = [AFHTTPResponseSerializer serializer]; } return self; }
它仅在每个应用程序会话中初始化一次。
更新:根据要求,这是userInfo
的error
中的userInfo
:
NSErrorFailingURLKey =“ https:// MY_URL / PARAMS ”; NSErrorFailingURLStringKey =“ https:// MY_URL / PARAMS ”; NSLocalizedDescription =已取消; NSUnderlyingError =“错误域= kCFErrorDomainCFNetwork代码= -999 \”(null)\“UserInfo = {_ kCFStreamErrorCodeKey = 89,_kCFStreamErrorDomainKey = 1}”; “_kCFStreamErrorCodeKey”= 89; “_kCFStreamErrorDomainKey”= 1;
经过更多的测试,我注意到如果我重新创建网络类,在每个网络调用上声明manager
– 我从来没有得到这个错误。
-999
或ErrorCancelled
初始化的管理器不归属,并在超出范围后立即解除分配。 因此,任何待处理的任务都将被取消。
使用AFHTTPSessionManager创建一个单例类,并将其用于所有请求。
YourAPIClient.h
#import "AFHTTPSessionManager.h" @interface YourAPIClient : AFHTTPSessionManager +(YourAPIClient *) sharedClient; @end
YourAPIClient.m
#import "YourAPIClient.h" @implementation YourAPIClient //Singleton Shared Client +(YourAPIClient *)sharedClient { static YourAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; // Initialize Session Manager _sharedClient = [[YourAPIClient alloc] initWithSessionConfiguration:sessionConfiguration]; // Configure Manager [_sharedClient setResponseSerializer:[AFHTTPResponseSerializer serializer]]; }); return _sharedClient; }
我们有类似的案例。 对于我们的特定情况,客户在公司网络内部使用应用程序,这是为每个请求注入公司的证书。 如果设备没有公司的自定义CA,则用户将收到此特定错误。
这似乎是iOS中的一个错误。 与HTTP load failed
关联的错误代码HTTP load failed
( CFErrorDomainCFNetwork
), 999
用于取消请求( cfurlErrorCancelled
)。
你自己回答了这个问题。 要使其在iOS 11上运行,您应该考虑为每个呼叫使用不同的管理器,因为看起来一个管理器上的网络请求彼此冲突。
参考资料:
https://github.com/AFNetworking/AFNetworking/issues/3999 https://developer.apple.com/documentation/cfnetwork/cfnetworkerrors/1416998-cfurlerrorcancelled iOS中的NSURLErrorDomain错误代码-999