ShatyUT
ShatyUT

Reputation: 1365

CoreLocation: How do I wake my app up when iPhone turned on after significant location change?

I am trying to build an app that relies on both region monitoring and significant location changes. The specific scenario I am testing is:

  1. A user (with my app) goes to the airport which has a region around it
  2. My app is successfully woken up due to the region breach...entry.
  3. They turn their iPhone off like a good little passenger.
  4. They land in some far away city
  5. They turn the phone back on

Should I expect to be provided with a new location in the background via significant changes monitoring? It's too expensive to take flights all the time to test this so I'm testing by turning my phone off, driving 5 or so miles and turning it back on.

Spoiler alert! I am not getting a new location when I turn my phone on. Maybe I need to be driving much farther but 5 miles should be plenty for cell tower change...even though it's not really a cell tower change since the phone was off.

Upvotes: 1

Views: 1351

Answers (2)

ShatyUT
ShatyUT

Reputation: 1365

It turns out that the app gets woken up in a very interesting way in this case. I found this paragraph in the doc for startMonitoringSignificantLocationChanges:

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 locationManager:didUpdateLocations: method of your application delegate contains the key UIApplicationLaunchOptionsLocationKey to indicate that your application was launched because of a location event. 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.

I've bolded what I think is a very important part. First, when the phone starts back up and the OS realizes it has moved a significant distance, it will NOT call locationManager:didUpdateLocations: as both I would have expected and it says in the doc. It will launch the app in the background calling the application:didFinishLaunchingWithOptions: method of the AppDelegate. The NSDictionary provided there will contain a special key (noted in the doc above) telling you it was launched due to a location change. It is then up to you to call locationManager:startMonitoringSignificantLocationChanges again.

Hope this helps someone else. I had made myself a simple little tester app to test this in a vacuum and verified it worked for me.

Upvotes: 4

Neal Ehardt
Neal Ehardt

Reputation: 10954

If you run your application while connected to Xcode's debugger, you can simulate location changes. This appears to work with either simulator or tethered device.

https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/simulating_locations.html

Upvotes: 0

Related Questions