1

I'm working on an application that should be able to collect location data while the application is in background.

To allow this, I have done the following:

  • In the project settings, under the Capabilities tab I have ticked Location updates. enter image description here

  • I have set the NSLocationAlwaysUsageDescription key in the Applications Info.plist and have assigned an appropriate string value.

  • I have configured an instance of CLLocationManager like so:
    • locationManager.activityType = .Fitness
    • locationManager.desiredAccuracy = kCLLocationAccuracyBest
    • locationManager.pausesLocationUpdatesAutomatically = false
  • Before beginning location collection I call locationManager.requestAlwaysAuthorization(), I have the appropriate delegate callbacks to handle any error.
  • lastly, I call locationManager.startUpdatingLocation()

When running the application on the simulator, I get a print out of the location updates, and the application's badge is also updated with the number of locations collected, but when running the applications on an actual device, after sending the app in the background, location updates are shortly stopped from being handled by the app (I know this as the badge stops updating).

Am I missing some sort of device specific configuration?

Gabor
  • 278
  • 4
  • 14
  • 1
    I had the same issue for a while and in my case it was because of [this](http://stackoverflow.com/questions/30808192/allowsbackgroundlocationupdates-in-cllocationmanager-in-ios9/30821461#30821461), basically `allowsBackgroundLocationUpdates` variable is false as default in iOS 9. Hope it helps. – ujell Oct 09 '15 at 11:59
  • @ujell thanks! that has solved the problem! – Gabor Oct 10 '15 at 09:08

2 Answers2

0

Here is my setting that works all right when the app is in the background or the device in standby mode:

    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.pausesLocationUpdatesAutomatically = false
    locationManager.activityType = .Other
    locationManager.startUpdatingLocation()

I am also using deferred location updates. In didUpdateLocations:

if CLLocationManager.deferredLocationUpdatesAvailable() && deferringUpdates == false {
            locationManager.allowDeferredLocationUpdatesUntilTraveled(CLLocationDistanceMax, timeout: deferredLocationTimeout)
            deferringUpdates = true
} 

and in didFinishDeferredUpdatesWithError:

deferringUpdates = false
MirekE
  • 11,515
  • 5
  • 35
  • 28
  • Thanks @MirekE, but I don't want to defer location updates. I want to process them in background as they come in. – Gabor Sep 08 '15 at 12:04
0

Setting the capabilities is not enough as of iOS 9. You need to also set locationManager.allowsBackgroundLocationUpdates = true on your Location Manager before OS can deliver updates to you in the background. It is compulsory to do so according to the documentation. By default it is set to false. See here.

zakishaheen
  • 5,551
  • 1
  • 22
  • 29