Article

Requesting Authorization for Location Services

Ask the user for permission to access their location.

Overview

It’s important to take great care with a user’s location data, and the system requires your app to get the user’s authorization to access this kind of information. To request authorization, call requestWhenInUseAuthorization() or requestAlwaysAuthorization(), depending on the authorization status your app needs. See Choosing the Location Services Authorization to Request for more information about deciding what type of authorization to request. Your Xcode project must be configured with purpose strings used in the authorization prompts.

Configure Purpose Strings in Xcode

To prepare your app for calling requestWhenInUseAuthorization() or requestAlwaysAuthorization(), you must add keys with purpose strings to your app's Info.plist file. The system displays your purpose strings in the authorization request dialogs. Configure the keys in Xcode with the purpose strings that your app requires.

Key

Required when:

NSLocationWhenInUseUsageDescription

Your app requests When In Use authorization or Always authorization.

NSLocationAlwaysAndWhenInUseUsageDescription

Your app requests Always authorization.

NSLocationUsageDescription

Your app runs in macOS and uses location services.

NSLocationAlwaysUsageDescription

Your app supports iOS 10 and earlier and requests Always authorization.

See the Human Interface Guidelines for more information about purpose strings.

Ask for Authorization During Location-Related Activities

Consider the context of the user's interactions when you request authorization to use their location.

  • Request authorization only when your user needs location services to perform a task in your app. If it's not clear to the user why your app is using location services, the user may deny your request.

  • Don't automatically ask for authorization when your app returns to the foreground. If your app has temporary authorization, it loses it when the user stops using your app. Unless the user expects you to continue using their location, wait until the user needs the location to be updated again before requesting authorization.

Call requestWhenInUseAuthorization() or requestAlwaysAuthorization() to request authorization at the point where you need location services. You may request When In Use authorization as needed; there are no limits on calling requestWhenInUseAuthorization(). The system does limit calls to requestAlwaysAuthorization(); you may request it only one time.

After making an authorization request, it’s safe to start location services before your request resolves. However, you won't receive location events until your app's authorization status changes to CLAuthorizationStatus.authorizedAlways or CLAuthorizationStatus.authorizedWhenInUse. Implement the locationManager(_:didChangeAuthorization:) method in your location manager delegate to be notified when your app's authorization status changes.

See Also

Authorization

Choosing the Location Services Authorization to Request

Determine the authorization your app needs to access location data.

Responding to Changes in Authorization Status

Receive and respond to changes in your app's authorization status in your delegate's method.

enum CLAuthorizationStatus

Constants indicating the app's authorization to use location services.

property list key NSLocationAlwaysAndWhenInUseUsageDescription

A message that tells the user why the app is requesting access to the user’s location information at all times.

Name: Privacy - Location Always and When In Use Usage Description
property list key NSLocationWhenInUseUsageDescription

A message that tells the user why the app is requesting access to the user’s location information while the app is running in the foreground.

Name: Privacy - Location When In Use Usage Description
property list key NSLocationUsageDescription

A message that tells the user why the app is requesting access to the user’s location information.

Name: Privacy - Location Usage Description
property list key NSLocationAlwaysUsageDescription

A message that tells the user why the app is requesting access to the user's location at all times.

Name: Privacy - Location Always Usage Description
Deprecated