Awesome.Apple
Awesome.Apple

Reputation: 1324

NSURLSessionDataTask failed with error code -999

I am using NSUrlSession for calling backend APIs. Sometimes the APIs are working but sometimes i am getting the issues like

HTTP load failed (error code: -999), HTTP load failed (error code: -1200)

Refer below :- Screenshot from Xcode logs. enter image description here

I have researched regarding it and the possible solutions suggested is to use App Transport Security Settings and I am already using it.

Here is the screenshot from info.plist file of my project. enter image description here

Here below is the code i am using :-

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
} downloadProgress:^(NSProgress * _Nonnull downloadProgress) {

} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    NSInteger statusCode = [httpResponse statusCode];
    completionBlock();
}];

[dataTask resume];

So not able to figure out what exactly causing NSURLSessionDataTask to fail. Even completion block is not called.

Any suggestions?

Thanks,

Upvotes: 0

Views: 650

Answers (1)

bguidolim
bguidolim

Reputation: 367

Error code -999 is cancelled request, as you're using a wrapper for NSURLSession, probably you can't see the correct message.

Anyways, you're instantiating 3 new objects: configuration, manager and data task. None of them has a strong reference in another object (a UIViewController for instance), so when you call [dataTask resume], ARC is already removing the reference of your object, because in the concept of sync code, this code is done.

In summary, this is the correct behaviour, to fix that, try to create a strong reference of AFURLSessionManager in your class and everything will be ok.

Upvotes: 3

Related Questions