Difference between different Location Requesting Types

Hi at all,


in iOS 11 there are two different types of location requesting. The plist Keys which are needed are NSLocationWhenInUseUsageDescription (which is at least required when you want to request the location, says the apple documentation, right?) and NSLocationAlwaysAndWhenInUseUsageDescription.


In the CLLocationManager class there are a requestWhenInUseAuthorization and a requestAlwaysAuthorization method. There is also a property called "allowsBackgroundLocationUpdates".


How this three possibilities, so the two methods for location requesting and the property do work together? When I have to call which method and what are the property for? When I request "requestAlwaysAuthorization" why should I also need to set the "allowsBackgroundLocationUpdates" to true? Or why I need this property?


Thanks in advance 🙂

J0nas

Replies

WHat I read in document seems to answer:


func requestWhenInUseAuthorization()

Requests permission to use location services while the app is in the foreground.


func requestAlwaysAuthorization()

Requests permission to use location services whenever the app is running.


So, if you want to receive background update, you need to request always.


In addition, you need to set allowsBackgroundLocationUpdates:

Apps that want to receive location updates when suspended must include the

UIBackgroundModes
key (with the
location
value) in their app’s
Info.plist
file and set the value of this property to
true
. The presence of the
UIBackgroundModes
key with the
location
value is required for background updates; you use this property to enable and disable background updates programmatically. For example, you might set this property to
true
only after the user enables features in your app where background updates are needed.

When the value of this property is

false
, apps receive location updates normally while running in either the foreground or background based on its current authorization. Updates stop only when the app is suspended, thereby preventing the app from being woken up to handle those events.

The default value of this property is

false
. Setting the value to
true
but omitting the
UIBackgroundModes
key and
location
value in your app’s
Info.plist
file is a programmer error.


Hope that answers.

Thanks for your answer, that helped me a lot.

Is it also right, that if the app ask for location access and the user gives only "WhenInUse" access you can access the location in background anyway but a blue indicator is shown on the status bar (the allowsBackgroundLocationUpdates property has also to be set on true, i think) when the app is in background. If the user gives "Always" access the blue indicator is not shown when the app is in the background and the location is accessed.


Is this the right behavior? I could get this behavior with a test app and only using the "requestAlwaysAuthorization" method for requesting.


Thanks in advance

J0nas