title: How to solve NSURLError: The network connection was lost?
body: <p>I am working on an application in which I am using web-services method to fetch data from server.</p>
  
  <p>Below is my code.</p>
  
  <pre><code>responseData = [NSMutableData data];
  
      NSString *service = @"/getActivity.php";
  
      NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"URLName" ofType:@"plist"];
      NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
      NSString *urlLoc = [fileContents objectForKey:@"URL"];
      urlLoc= [urlLoc stringByAppendingString:service];
  
      loginUser = [[NSUserDefaults standardUserDefaults]valueForKey:@"username"];
      NSLog(@"%@",loginUser);
  
      NSLog(@"%@",selectUsername);
      // this is correct
      //NSString *requestStringg = [NSString stringWithFormat:@"{\"?username\"=\"%@\"&amp;\"follower_username\"=\"%@\"}",loginUser,selectUsername];
      NSDate * date = [NSDate date];
      // NSLog(@"Date:%@",date);
      NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
  
      [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  
      NSString *date_str = [dateFormat stringFromDate:date];
  
      NSString *requestString = [NSString stringWithFormat:@"username=%@&amp;follower_username=%@&amp;follower_created_date=%@",loginUser,selectUsername,date_str];
  
      NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
      NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  
      [request setURL:[NSURL URLWithString:urlLoc]];
      [request setHTTPMethod:@"POST"];
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
      [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
      [request setHTTPBody:postData];
  
      postFollowUnfollowConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
  
  - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
  {
      [responseData setLength:0];
  }
  
  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      [responseData appendData:data];
  }
  
  - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
  {
  
      NSLog(@"error :%@",error);
  
  }
  
  - (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {
  
      if (connection == postNotificationConnection)
      {
          NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
          NSLog(@"%@",responseString);
  
       }
  
  }
  </code></pre>
  
  <p>My web-services are working fine, but after the 4th or 5th time of calling web-services it enters into the <code>didFailWithError</code>, showing me the below error.</p>
  
  <p><a href="https://i.stack.imgur.com/Synp7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Synp7.png" alt="enter image description here"></a></p>
  
  <p>I have tried every possible solution for this error, but nothing helps me. This error is being produced in iOS 8 and iOS 9. </p>
  
  <p>Any help would be appreciated.</p>