Reputation: 1365
I am trying to build an app that relies on both region monitoring and significant location changes. The specific scenario I am testing is:
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
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
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.
Upvotes: 0