Bojan
Bojan

Reputation: 147

CLLocationManager while app is in background state

My question is: Is CLLocationManager continue running, while my app is inactive?

Upvotes: 6

Views: 8053

Answers (4)

mattv123
mattv123

Reputation: 979

There are some important subtleties with this (as of iOS 7.1):

  • The Location Update background mode should NOT be used if you are just looking for significant change and region enter/exit events. You will still receive these events even if the background flag is NOT set, and you will save a lot of battery at the same time.
  • If you do the above, you need to be mindful of limited permitted background time. If you don't take care to wrap up network requests etc. in the permitted time, you will network transaction failures.
  • You should ONLY use the location background mode if you need to use detailed location tracking (e.g. -startUpdatingLocation), in which case this background mode will keep your app awake.
  • Using the location background mode when not getting detailed location will piss off your users and may get your app rejected during the review process (depending on how you use location throughout your app).
  • Your app may be killed at anytime by the OS if you do not have the background location mode set, so you will need to be sure to properly re-initialize your CLLocationManager instance in applicationDidFinishLaunching or applicationWillFinishLaunching in order to get the subsequent updateLocation or didEnter/ExitRegion delegate call. Just because location wakes up your app with a location update, it does NOT magically re-create your CLLocationManager without you programming it!

Hope that helps!

Upvotes: 2

Magurizio
Magurizio

Reputation: 300

To disable CLLocationManager while the app is in backround mode you simply must not add the "App registers for location updates" in the "Required background modes" key of the info.plist file.

I suggest to use the significant-change location service instead of the standard location service whenever possible, to preserve device battery.

Upvotes: 1

mservidio
mservidio

Reputation: 13057

Yes, it could. You have two options for handling location service events when your app is suspended, which can be read at the article: Getting the User’s Current Location. As noted:

There are two different services you can use to get the user’s current location:

  • The standard location service is a configurable, general-purpose solution and is supported in all versions of iOS.
  • The significant-change location service offers a low-power location service for devices with cellular radios. This service is available only in iOS 4.0 and later and can also wake up an application that is suspended or not running.

Also, as noted at the bottom of this article in the section "Getting Location Events in the Background":

  • If your application needs location updates delivered whether the application is in the foreground or background, there are multiple options for doing so. The preferred option is to use the significant location change service to wake your application at appropriate times to handle new events. However, if your application needs to use the standard location service, you can declare your application as needing background location services.
  • An application should request background location services only if the absence of those services would impair its ability to operate. In addition, any application that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation application would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.

Upvotes: 2

graver
graver

Reputation: 15213

Yes, if CLLocationManager is first called startUpdatingLocation method, and in the AppName-Info.plist file is added Required Background Modes -> App registers for location updates

Upvotes: 9

Related Questions