Question

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)?

Was it helpful?

Solution

When the app is subsequently terminated, does the CLLocationManager keep monitoring for the changes?
-> you guess is right. it's No, but your os monitors and knows that this application might need it because you called startMonitoringSignificantLocationChanges:.

your question -I know that next the app gets relaunched with UIApplicationLaunchOptionsLocationKey passed to application:didFinishLaunchingWithOptions:. Is it done in the background?

Ans-> yes,

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.

what delegate does get called when there is a new location event and the app is running (either in the background or foreground)?

The link you provided says everything. when your app is running in foreground or backGround, and if a new location event arrives , and your app app to become active,still your CLLocationManagerDelegate will get called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top