0

I am working on an application with xcode 9.2 and iOS 11.4 . When i try NSURLSession downloadTaskWithRequest in background and foreground switching of the application state some times the app get 200 success with Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”

The error is as follows :

NSLocalizedDescription = "The network connection was lost."; NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1005 \"(null)\" UserInfo={NSErrorPeerAddressKey={length = 28, capacity = 28, bytes = 0x1c1e01bb000000002604558000210000 ... a5c562bc00000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}"; "_kCFStreamErrorCodeKey" = 57; "_kCFStreamErrorDomainKey" = 1;

My request is as follows :

NSMutableURLRequest *urlRequest = nil;
    NSString *urlString = nil;
    NSURL * url = nil;

    urlString = [NSString stringWithFormat:@"https://%@%@", self.requestUrl, self.queueName];
    url = [NSURL URLWithString:urlString];
    urlRequest = [NSMutableURLRequest requestWithURL:url];
    urlRequest.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

    [urlRequest addValue:@"" forHTTPHeaderField:@"User-Agent";

[urlRequest setHTTPMethod:@"DELETE"];
            [urlRequest setTimeoutInterval:REQUEST_TIMEOUT];
            [urlRequest addValue:@"correlID, msgId, usr-clientMessageId" forHTTPHeaderField:@"x-msg-require-headers"];
            [urlRequest addValue:self.messageInfo.messageId  forHTTPHeaderField:@"x-msg-msgId"];
            [urlRequest addValue:@"Delivery"  forHTTPHeaderField:@"applicationOrgin"];
             [urlRequest addValue:@"" forHTTPHeaderField: @"x-msg-wait";

And the downloadTaskWithRequest code snippet is as follows :

self.downloadTask = [self.urlSession downloadTaskWithRequest:urlRequest completionHandler:
                             ^(NSURL *location, NSURLResponse *response, NSError *error){
                                 FLog(@"received response for message type: %d and operation type: %d\n",self.messageType, self.requestType);
                                 NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
                                 FLog(@"%ld for operation %d for request type %d",(long)[httpResponse statusCode],self.self.messageType,self.requestType);
                                 FLog(@"response is  %@",response);
                                 NSHTTPURLResponse *httpresp = (NSHTTPURLResponse *)response;
                                 NSString * origId = [[httpresp allHeaderFields] objectForKey:(@"x-msg-msgId")];
                                 FLog(@"recieved response header %@",httpresp.allHeaderFields);
                                 [self downloadCompletedWithResponse:response location:location error:error];
                             }];
        [self.downloadTask resume];

Is anybody facing the same issue? Could any body having idea to resolve this issue

  • Do you have any special header on your request? I had the same issue with this error code (-1005), see this post: https://stackoverflow.com/questions/50678114/the-network-connection-was-lost-error-domain-kcferrordomaincfnetwork-code-100 – Reza Dehnavi Oct 09 '18 at 20:43
  • Thanks Reza for the quick update. I checked my request , it doesn't contain special headers like "Content-length". – user1820190 Oct 10 '18 at 12:35

0 Answers0