I was reading up on application:didFinishLaunchingWithOptions: method and came across the following code from NSHipster:
@import CoreLocation;
@interface AppDelegate () <CLLocationManagerDelegate>
@property (readwrite, nonatomic, strong) CLLocationManager *locationManager;
@end
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
if (![CLLocationManager locationServicesEnabled]) {
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Location Services Disabled", nil)
message:NSLocalizedString(@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled.", nil)
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil] show];
} else {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
}
if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {
[self.locationManager startUpdatingLocation];
}
}
I've already looked into this question, but I am still confused about the sequence of events. Correct me if I'm wrong:
When an app is launched for the first time, the CLLocationManager
gets set-up (if the location services are enabled) and it starts monitoring for the significant changes in location. When the app is subsequently terminated, does the CLLocationManager
keep monitoring for the changes? As far as I can tell, it does not. Then how does it get launched if there is a new location event? I know that next the app gets relaunched with UIApplicationLaunchOptionsLocationKey
passed to application:didFinishLaunchingWithOptions:
. Is it done in the background? Now the CLLocationManager
will get set-up (again) first, and right after that the locationManager
will start updating the location as per event. After that the CLLocationManager
will keep monitoring for the changes.
Related question: what delegate does get called when there is a new location event and the app is running (either in the background or foreground)?