AFNetworking iOS11-HTTP加载失败-999 [英] AFNetworking iOS11 - HTTP load failed -999

查看:208
本文介绍了AFNetworking iOS11-HTTP加载失败-999的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在生产环境中运行了一段时间的应用程序。但是,由于iOS 11 –我收到此错误:

I have an app in production that has been running fine for a while. However, since iOS 11 - I am receiving this error:


任务< 51E07EF5-6F31-4E58-8818-7712CB3E49A6>。 20> HTTP加载失败(错误代码:-999 [1:89])
任务< 51E07EF5-6F31-4E58-8818-7712CB3E49A6>。< 20>完成,错误-代码:-999

Task <51E07EF5-6F31-4E58-8818-7712CB3E49A6>.<20> HTTP load failed (error code: -999 [1:89]) Task <51E07EF5-6F31-4E58-8818-7712CB3E49A6>.<20> finished with error - code: -999

这仅在iOS 11上发生。另外,它也不是一致的。对同一URL的五个请求中,有两个将因此失败。在蜂窝连接上,它发生的频率较低,但仍然存在。

This happens on iOS 11 only. Also, it is not consistent. Out of five requests to the same URL, two will fail with this. On a Cellular connection, it happens less often, but its still there.

我正在使用AFNetworking,并且如上所述;直到iOS11,这才成为问题。

I am using AFNetworking and as said; this has never been an issue until iOS11.

有什么想法吗?

更新以添加更多详细信息:

Update to add more details:

我正在使用带有有效证书的SSL固定。我的info.plist看起来像这样:

I am using SSL Pinning with valid certificates. My info.plist looks like this:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>url.com</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <false/>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict>

使用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];
        }

更新:
注释:这是管理器的声明方式:

Update: In response to the comments: this is how the manager is declared:

@property (nonatomic) AFHTTPSessionManager *manager;

管理器是通过我的Web服务类的init方法初始化的:

The manager is initialised through an init method of my web service class:

-(instancetype)initWithBaseUrl:(NSURL *)url {

    self  = [super init];
    if (self){
        self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url];
        self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    }

    return self;
}

每个应用会话仅初始化一次。

It is only initialised once per app session.

更新:
根据要求,这是 userInfo 错误再次出现:


NSErrorFailingURLKey = https:// MY_URL / PARAMS
NSErrorFailingURLStringKey = https:// MY_URL / PARAMS
NSLocalizedDescription =已取消;
NSUnderlyingError =错误域= kCFErrorDomainCFNetwork代码= -999 \(空)\ UserInfo = {_ kCFStreamErrorCodeKey = 89,
_kCFStreamErrorDomainKey = 1};
_kCFStreamErrorCodeKey = 89;
_kCFStreamErrorDomainKey = 1;

NSErrorFailingURLKey = "https://MY_URL/PARAMS"; NSErrorFailingURLStringKey = "https://MY_URL/PARAMS"; NSLocalizedDescription = cancelled; NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-999 \"(null)\" UserInfo={_kCFStreamErrorCodeKey=89, _kCFStreamErrorDomainKey=1}"; "_kCFStreamErrorCodeKey" = 89; "_kCFStreamErrorDomainKey" = 1;

在进行更多测试后,我注意到,如果重新创建网络类,在每个网络调用中都声明了 manager -我从没有收到此错误。

Upon more testing, I have noticed that if I recreate the network class, where the manageris declared - on each network call - I never get this error.

推荐答案

-999 ErrorCancelled

初始化后的管理器不属于所有者,并且在超出范围后不久便被释放。因此,所有待处理的任务都会被取消。

The initialized manager is not owned, and is deallocated shortly after it goes out of scope. As a result, any pending tasks are cancelled.

使用AFHTTPSessionManager创建单例类,并将其用于所有请求。

Create a singleton class with AFHTTPSessionManager and use that for all your requests.

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;
}

这篇关于AFNetworking iOS11-HTTP加载失败-999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆