Enabling Location - iOS

To use location on iOS, first thing is enabling associated services on the user device. Find out here how to do it and more importantly what are the different permissions and consequences of choices made by the user.
  1. Setting Up Core Location
  2. Allow Once Permission
  3. “Always Allow” Hidden in Allow While Using App Permission
  4. Request Authorization

Setting Up Core Location

Location Permissions

The sdk needs some permissions to work. To be able to take background position readings you will need the whenInUse permission. If you want to take readings permanently the always permissions will be required.

In these two cases you will also need to activate background mode in Xcode.

Background Mode

Since iOS 10 it’s mandatory to provide a usage description in the info.plist if trying to access privacy-sensitive data. When the system prompts the user to allow access, this usage description string will be displayed as part of the permission dialog box, but if you didn’t provide the usage description, the app will crash before showing the dialog. Also, Apple will reject apps that access private data but don’t provide a usage description.

This SDK require the following usage description:

These descriptions can be configured in the info.plist file, by adding the following keys:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This text is shown when permission to use the location of the device is requested. Note that for the app to be accepted in the App Store, the description why the app needs location services must be clear to the end user.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This text is shown when permission to use the location of the device is requested. Note that for the app to be accepted in the App Store, the description why the app needs location services must be clear to the end user.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This text is shown when permission to use the location of the device is requested. Note that for the app to be accepted in the App Store, the description why the app needs location services must be clear to the end user.</string>

Here’s the code to get you started with Core Location in your iOS application:

var locationManager = CLLocationManager()  
locationManager.requestAlwaysAuthorization()

For allowsBackgroundLocationUpdates, ensure that you’ve enabled the Background mode location from the Capabilities in your Xcode project.

iOS 13 has the following three location permissions (ignore Denied since it ignores the permission):

Allow Once Permission

Allow Once is similar to Allow While Using, but only for one foreground session. That means that once you leave the application for a considerable time, the permission state changes to notDetermined.

The next time the user starts the application, the developer can ask for the permissions again, depending on their use case. This gives users some finer control over the location data and also allows the developers to handle on/off location cases easily.

“Always Allow” Hidden in Allow While Using App Permission

Allow While Using App permission defers the Always Allow permission.

Always Allow permission isn’t there by default in the new permission dialog. It’s there in a provisional form so that it can be used when it’s actually required.

Let’s see how that works with the different kinds of location authorizations.

Prompt flow

If user will press the best positive answer in that case which is Allow While Using App the operating system will remember that.System will present user alert at some point of time when some special event occurs. System will present alert with Keep Only While Using and Change to Always Allow options. For more information you can check on video from WWDC 2019 : https://developer.apple.com/videos/play/wwdc2019/705/

Request Authorization

You can request of two authorization cases.

requestAlwaysAuthorization

requestWhenInUseAuthorization