allowsBackgroundLocationUpdates clarifications

In our app we have the necessity to gather background location after a trigger from a bluetooth event. During the onboarding we ask for authorizedWhenInUse. CLLocationManageris configured like this, and in capabilities we provided the location flag.

         let cl = CLLocationManager()
        cl.delegate = self
        cl.allowsBackgroundLocationUpdates = true
        cl.desiredAccuracy = kCLLocationAccuracyBestForNavigation

According to apple doc or at least what I understood, this should be enough to obtain locations even when the app is in background (and awake).

The system allows background updates for apps with either When in Use or Always authorization. If an app isn't running when an update occurs, the system launches the app only if it has Always authorization and uses the significant location change, visits, or region monitoring services.

We saw an inconsistent bahviour, the system request to start the location updates after the bluetooth event (we have background capabilities set also for that). In foreground it work always, but in background sometimes it works and sometimes not. When we don't receive a location we usually get a CLError.denied in the func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) method.

At this point I don't get the apple doc quote, is it possible to obtain location in background or not with authorizedWhenInUse?

Replies

I think that if you have whenInUse auth, you can start getting updates in the foreground, then go to the background and continue to get them.

Turning locations on while in the background may need the always auth.

I take it you are using a bluetooth background mode in order to be running in the background with locations off, right? This is an unusual situation.

  • I was thinking the same, but the app is somehow always awaken and running even in background due to bluetooth activity.

Add a Comment