Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does iOS knows which location manager delegate to call on region monitoring while the app is not running?

Tags:

ios

I might be confusing how region monitoring works, but this is what i have so far:

I am registering a region to monitor through my location manager, which is implemented on a singleton class, this singleton is also set as the delegate of the location manager so the implemented method is being called.

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region

This works totally as expected, if the app is active or suspended the method is being called. It also makes total sense because the class has been already loaded and when the region enter event occurs iOS sends this even to my app which calls the location manager who registered (probably has a reference to it) and in turn it calls whatever delegate was also registered along it (since the class is there ready and loaded).

The issue is, what happens when the app has been killed? Is it first launched into the background? How does iOS know what delegate method to call, and if it has already been loaded?

like image 243
Pochi Avatar asked Jul 25 '13 06:07

Pochi


People also ask

What is geofencing in iOS?

Overview. Region monitoring (also known as geofencing) is a way for your app to be alerted when the user enters or exits a geographical region. You might use region monitoring to perform location-related tasks.

What is CLLocation manager in Swift?

The object that you use to start and stop the delivery of location-related events to your app.

What is CLLocation in Swift?

Overview. A CLLocation object contains the geographical location and altitude of a device, along with values indicating the accuracy of those measurements and when they were collected. In iOS, a location object also contains course information — that is, the speed and heading in which the device was moving.


1 Answers

When your app has been killed and gets started for a location update there can't be a location manager delegate yet and as such there are no notifications delivered to that delegate. The system can't know which of your classes should be used as a location manager delegate or how to instantiate it.

Instead your application:didFinishLaunchingWithOptions: gets called as usual, but the UIApplicationLaunchOptionsLocationKey is set in the options dictionary. That tells your app that you need to instantiate a location manager and set it's delegate. Only after you did this the delegate gets called with the region updates.

like image 94
Sven Avatar answered Sep 21 '22 02:09

Sven