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?