title: sending current location to server in background as well as in running app using AFNetworking 3
body: <p>I tried to search lot for this, but i didn't get exact answer what i want.</p>
  
  <p><strong>my question is</strong> </p>
  
  <p>Can I send user's current location to server when app is running as well as in background. </p>
  
  <p><strong>For example:</strong> In my app i want to store the user's current location in every 2min and then send his current location to server.as well as if user move 50 meters within less than 2min then also.
  How can i do this ? Can i send location updates to the server in background mode as well as foreground mode? if yes then how?</p>
  
  <p>I tried like following way:</p>
  
  <pre><code> self.locations = [[NSMutableArray alloc] init];
  self.locationManager = [[CLLocationManager alloc] init];
  self.locationManager.distanceFilter = 10;
  
  self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  self.locationManager.delegate = self;
  [self.locationManager requestAlwaysAuthorization];
  
  [self.locationManager startUpdatingLocation];
  </code></pre>
  
  <p>...</p>
  
  <pre><code>- (void)locationManager:(CLLocationManager *)manager
  didUpdateToLocation:(CLLocation *)newLocation
         fromLocation:(CLLocation *)oldLocation
  {
  
  
  
   // Add another annotation to the map.
  MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
  annotation.coordinate = newLocation.coordinate;
  [self.map addAnnotation:annotation];
  
  // Also add to our map so we can remove old values later
  [self.locations addObject:annotation];
  
  // Remove values if the array is too big
  while (self.locations.count &gt; 1)
  {
      annotation = [self.locations objectAtIndex:0];
      [self.locations removeObjectAtIndex:0];
  
      // Also remove from the map
      [self.map removeAnnotation:annotation];
  }
  
  
  if (UIApplication.sharedApplication.applicationState == UIApplicationStateActive)
  {
      // determine the region the points span so we can update our map's zoom.
      double maxLat = -91;
      double minLat =  91;
      double maxLon = -181;
      double minLon =  181;
  
      for (MKPointAnnotation *annotation in self.locations)
      {
          CLLocationCoordinate2D coordinate = annotation.coordinate;
  
          if (coordinate.latitude &gt; maxLat)
              maxLat = coordinate.latitude;
          if (coordinate.latitude &lt; minLat)
              minLat = coordinate.latitude;
  
          if (coordinate.longitude &gt; maxLon)
              maxLon = coordinate.longitude;
          if (coordinate.longitude &lt; minLon)
              minLon = coordinate.longitude;
      }
  
      MKCoordinateRegion region;
      region.span.latitudeDelta  = (maxLat +  90) - (minLat +  90);
      region.span.longitudeDelta = (maxLon + 180) - (minLon + 180);
  
      _latitude1=[NSString stringWithFormat:@"Latitude:%f",newLocation.coordinate.latitude];
      _longitude1=[NSString stringWithFormat:@"Longitude:%f",newLocation.coordinate.longitude];
  
  
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
      appDelegate.fetchtokenString1 = [NSString stringWithString:_latitude1];
      appDelegate.fetchedriveridString1 = [NSString stringWithString:_longitude1];
  
  
  
  
  
      NSLog(@"%@",_latitude1);
      NSLog(@"%@",_longitude1);
  
  
      // the center point is the average of the max and mins
      region.center.latitude  = minLat + region.span.latitudeDelta / 2;
      region.center.longitude = minLon + region.span.longitudeDelta / 2;
  
      // Set the region of the map.
      [self.map setRegion:region animated:YES];
  }
  else
  {
      NSLog(@"App is backgrounded. New location is %@", newLocation);
  }
  </code></pre>
  
  <p>Thanks in advance..</p>