title: Sending Latitude and Longitude to Server when app is in background
body: <p>I have gone through so many links, even after that I haven't found a proper solution for getting latitude and longitude. </p>
  
  <p><a href="https://stackoverflow.com/questions/19042894/periodic-ios-background-location-updates">Periodic iOS background location updates</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15650139/ios-long-running-background-timer-with-location-background-mode">iOS long-running background timer with &quot;location&quot; background mode</a></p>
  
  <p>I tried from some links and forums but it is working for only 3 mins, then app is not at all updating the user location. </p>
  
  <pre><code>- (void)applicationDidEnterBackground:(UIApplication *)application {
  // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  
  
  //create new uiBackgroundTask
  __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
      [app endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
  }];
  
  //and create new timer with async call:
  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  
      [locationManager startUpdatingLocation];
  
      NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES];
      [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];
      [[NSRunLoop currentRunLoop] run];
  });
  
  }
   -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  {
  //  store data
  CLLocation *newLocation = [locations lastObject];
  
  
  //tell the centralManager that you want to deferred this updatedLocation
  if (_isBackgroundMode &amp;&amp; !_deferringUpdates)
  {
      _deferringUpdates = YES;
      [locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
  }
  }
  </code></pre>