Using your classes, I will do this.
In the AppDelegate.m application, when your application is in the foreground or in the background, I moved the CLLocationManager to run in the foreground / background to match. The reason I'm doing this is because if the CLLocationManager does not move to the background while the application is in the background, no location updates are sent to the CLLocationManager callback
- (void)applicationDidEnterBackground:(UIApplication *) application { [[LocationController sharedInstance] stop]; [[LocationController sharedInstance] startMonitoringSignificantLocationChanges]; NSLog(@"entered background Mode"); } - (void)applicationDidBecomeActive:(UIApplication *) application { [[LocationController sharedInstance] stopMonitoringSignificantLocationChanges]; [[LocationController sharedInstance] start]; NSLog(@"application Did Become Active"); }
So, let's say your application then moves to the background, and after a while, iOS decides to use it too much memory and kills your application.
After a few minutes, iOS then receives a location update and updates your application, letting it know that it appeared due to the location service. Then you need to restart the background location service, as that will be the only opportunity your application should do.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) { [[LocationController sharedInstance] startMonitoringSignificantLocationChanges]; } return YES; }
Oh, and the last change, I'm not sure why in your locationManager:didUpdateToLocation:fromLocation: you stop the location service, since when you do this, no more updates happen. Just leave it to work, and every time a change of location occurs, you can send it to the server.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 30) { self.lastLocation = newLocation; [self updateLocation];