Note that there are some explanatory texts on larger screens.

plurals
  1. POrecieving location updates after app is terminated
    text
    copied!<p>I need to keep track of the user location all the time (but not drain the battery). I understand the only way to get updates after app is terminated is using startMonitoringSignificantLocationChanges.</p> <p>From Apple's Location Awareness Programming Guide on startMonitoringSignificantLocationChanges:</p> <blockquote> <p>If you start this service and your application is subsequently terminated, the system automatically relaunches the application into the background if a new event arrives. In such a case, the options dictionary passed to the application:didFinishLaunchingWithOptions: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. <strong>Upon relaunch, you must still configure a location manager object and call this method to continue receiving location events. When you restart location services, the current event is delivered to your delegate immediately. In addition, the location property of your location manager object is populated with the most recent location object even before you start location services.</strong></p> </blockquote> <p>I would be glad if someone could demonstrate in the code (give an example) which methods i should use</p> <p>In the following code i'm tring to : - start the location manager at appdelegate which strats the signinficant monitor changes update and startupdating. - in didUpdateToLocation i'm calling stopupdating - in didFinishLaunchingWithOptions when i check if i got a UIApplicationLaunchOptionsLocationKey in order to know if i'm in the background and launched due to siginificant monitor location update. - if so, i call startMonitoringSignificantLocationChanges again (not sure why...) and begin a UIBackgeoundTaskIdentifier for calling startupdating method.</p> <pre><code>LocationController.m : + (LocationController*)sharedInstance { @synchronized(self) { if (sharedCLDelegate == nil) { [[self alloc] init]; } } return sharedCLDelegate; } - (id)init { self = [super init]; if (self != nil) { self.locationManager = [[[CLLocationManager alloc] init] autorelease]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [self.locationManager startUpdatingLocation]; [self.locationManager startMonitoringSignificantLocationChanges]; } return self; } - (void) startMonitoringSignificantLocationChanges { [self.locationManager startMonitoringSignificantLocationChanges]; } - (void) stopMonitoringSignificantLocationChanges { [self.locationManager stopMonitoringSignificantLocationChanges]; } -(void) start{ [self.locationManager startUpdatingLocation]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) &lt; 30) { self.lastLocation = newLocation; [self updateLocation]; //sending location to server [self.locationManager stopUpdatingLocation]; } } - (void)locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error{ [self.locationManager stopUpdatingLocation]; } AppDelegate.h : @interface AppDelegate : NSObject &lt;UIApplicationDelegate&gt; { UIBackgroundTaskIdentifier bgTask; } AppDelegate.m : - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]; if (locationValue) { [[LocationController sharedInstance] startMonitoringSignificantLocationChanges]; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; [[LocationController sharedInstance] start]; //startupdating return YES; } else { [[LocationController sharedInstance] init]; } } -(void) applicationDidEnterBackground:(UIApplication *) application { NSLog(@"entered background Mode"); } -(void) applicationDidBecomeActive:(UIApplication *) application { NSLog(@"application Did Become Active"); } </code></pre> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload