0
votes

I have built an app using Xcode 8.3.2 on iOS 10.3.2. And I have used NSURLSession to implement the webservices. I'm using the following code to do it.

NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:Url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:180.0] ;

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:lati forKey:@"latitude"];
[dict setObject:longi forKey:@"longitude"];

NSError *errJsonToData = nil;
NSData *requestPostData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&errJsonToData] ; 

[request setHTTPMethod:@"POST"] ;
[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)[requestPostData length]] ;forHTTPHeaderField:@"Content-Length"] ;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestPostData] ;


NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session=[NSURLSession sessionWithConfiguration:sessionConfiguration delegate:nil delegateQueue:[NSOperationQueue mainQueue]];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable connectionError) {
    if(connectionError)
    {
      NSLog(@"Error Description: %@",[connectionError localizedDescription]);
    } else { //do anything}
}];
[dataTask resume];

Now My webservices urls are http connection. For this I have also added App Transport Security in Info.plist. But the problem is this code works fine when connected to wifi but throws error sometimes while connected to mobile network i.e 3G, LTE. I get the error "The network connection was lost." The error is as follows:

Error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7ba8e5b0 {NSErrorFailingURLStringKey=, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7a6957e0 "The network connection was lost."}

If it is a typical server error then why its only happening on mobile network?

1
Are you able to try with a different carrier, or at least on another phone? It's likely that the error is correct, and you just need to have logic in place to handle it. In the real world, users will experience this anyways.CodeBender
@CodeBender : We tried different phones with AT&T, Verizon and Sprint carrier. But same result.Poles
Ok, it is also possible that you have a poor area for performance, but it is interesting seeing all the carriers since they rarely all share infrastructure. Have you looked into something like the Reachability framework to monitor connection status on the devices?CodeBender
@CodeBender : No...The users confirmed me that they have strong LTE connection. Yes I have checked. The problem is other APIs are working fine but this particular API which fetch data from a huge table on peak time (When all users accesing the same API) gives error only in mobile network.Poles
Interestingly, I didn't face the problem while on >=iOS 10.0. Do you think its a bug on Xocde 8.3.2 on iOS 10.3.2?Poles

1 Answers

0
votes

Depending on what server you're using, it is possible that the issue is with the "KeepAlive" setting on your server (usually due to higher traffic to the server than expected). Try catching for that specific error code and then re-attempt the exact same URL Request. Hopefully it will just work, but if it has that same error a second time, then there is a bigger issue that needs to be solved.

You can also try looking at the KeepAlive settings on your server and either disable the feature or tweak the settings, but I suggest trying that other option first to narrow down some possible issues.

Good luck!