7

Greetings all.

I am attempting to implement significant location change and region support into my app. While the app is active, there is obviously no problem receiving location updates. My question is how to handle updates when the app is not active.

This is my understanding of what happens if a significant location change or region entry/exit is detected:

  • If the app is in the background or suspended, iOS calls locationManager:didUpdateToLocation:fromLocation: (or locationManager:didEnterRegion:) on my existing location delegate.
  • If the app is terminated, iOS calls application:didFinishLaunchingWithOptions: with the key UIApplicationLaunchOptionsLocationKey on my application delegate. At this point I need to create a new location manager instance to obtain the new location.

Is this correct? Am I missing anything?

Thanks for help.

Regards, --John

johnnyspo
  • 619
  • 3
  • 7
  • 18

3 Answers3

8

You are partly right.

If the app is in background, and you are using significant location change:

  1. The app will call locationManager:locationDidUpdateToLocation:fromLocation
  2. If app crashed while it was in background, it will call application:didFinishLaunchingWithOption: with UIApplicationLaunchOptionLocationKey. You then have to init location manager again to get significant location change. This will then come in to locationManager:locationDidUpdateToLocation:fromLocation. This step is important

If the app is in background, and you are using region monitoring

  1. locationManager:locationDidUpdateToLocation:fromLocation will not be called
  2. the app calls locationManager:didEnterRegion:
honcheng
  • 2,014
  • 13
  • 14
  • Right. I left out locationManager:didEnterRegion. I think I'm giving up on region monitoring, seems to not work. Tried all size regions with various thresholds, can't seem to get it to work. – johnnyspo May 03 '11 at 16:56
  • @honcheng hi, if the app is in background locationManager:didEnterRegion will be called? really? I have read tons of posts and they all claim once the App is in background even if Region Monitoring is set, only locationManager:locationDidUpdate:fromLocation will be called. Just hope to clarify, because I would like to set Region Monitoring in the background. – Unheilig Jul 14 '13 at 14:43
  • Here is the solution on how to get the location update when the app is kill/suspended for iOS 7 and 8: http://stackoverflow.com/questions/27742677/how-to-get-location-updates-for-ios-7-and-8-even-when-the-app-is-suspended – Ricky Jan 02 '15 at 13:07
3

To get location update when the app is in the background versus when the app is suspended are 2 very different scenario. You will have to handle them differently.

You can only use the key UIApplicationLaunchOptionsLocationKey if your locationManager is using the method startMonitoringSignificantLocationChanges, you can not use startUpdatingLocation.

To get the location update when

A) The app is In the background, please see: Background Location Services not working in iOS 7

B) The app is suspended/terminated, please see: How to Get Location Updates for iOS 7 and 8 Even when the App is Suspended

I have written 2 very long article explaining the different between the 2 scenario. The source codes for the above 2 scenario are also available on the GitHub.

Community
  • 1
  • 1
Ricky
  • 10,485
  • 6
  • 36
  • 49
-3

I believe it always calls application: didFinishLaunchingWithOptions if your app is not in the foreground.

onnoweb
  • 3,038
  • 22
  • 29
  • 2
    It will only call this in background, if the app was not started, or had crashed. The device tries to restart the app, so it only calls this method then – honcheng Apr 28 '11 at 02:25