Article

Requesting When-In-Use Authorization

Request authorization to use location services only when your app is running.

Overview

To configure when-in-use authorization for location services, follow these steps (Listing 1 demonstrates steps 2 and 3.):

  1. Add the NSLocationWhenInUseUsageDescription key to your app's Information Property List file. (Xcode displays this key as "Privacy - Location When In Use Usage Description" in the property list editor.)

  2. Create and configure your CLLocationManager object.

  3. Call the requestWhenInUseAuthorization() method of your CLLocationManager object prior to starting any location services.

Request authorization at the point where you need location services. For example, wait until the user activates a feature of your app that requires location data. Don't request authorization at launch time unless you need the user's location immediately or your app was launched in the background by the system to handle a location update.

Request When-In-Use Authorization

Listing 1 shows how you might request when-in-use authorization before starting your app's location-related services, by calling the requestWhenInUseAuthorization() method. You need to request authorization only when your app's authorization status is not determined. If your app's authorization status is already determined, you can simply start the services that you need.

Listing 1

Requesting authorization to use location services

let locationManager = CLLocationManager()
func enableBasicLocationServices() {
   locationManager.delegate = self
        
   switch CLLocationManager.authorizationStatus() {
      case .notDetermined:
         // Request when-in-use authorization initially
         locationManager.requestWhenInUseAuthorization()
         break
            
      case .restricted, .denied:
         // Disable location features
         disableMyLocationBasedFeatures()
         break
            
      case .authorizedWhenInUse, .authorizedAlways:
         // Enable location features
         enableMyWhenInUseFeatures()
         break
      }
   }
}

Respond to Changes in Authorization Status

When you make an authorization request, Core Location processes your request asynchronously and delivers the results to the locationManager(_:didChangeAuthorization:) method of your location manager's delegate object. Use that method to determine whether the user granted or denied your request, and to take any appropriate actions in response.

Listing 2 shows an implementation of the locationManager(_:didChangeAuthorization:) method that enables the app's location-related features when the app receives authorization and disables those features when authorization is denied or restricted for any reason.

Listing 2

Getting changes to the app's authorization

func locationManager(_ manager: CLLocationManager, 
                      didChangeAuthorization status: CLAuthorizationStatus) {
   switch status {
      case .restricted, .denied:
         disableMyLocationBasedFeatures()
         break
            
      case .authorizedWhenInUse:
         enableMyWhenInUseFeatures()
         break
                  
      case .notDetermined, .authorizedAlways:
         break
   }
}

Topics

Purpose Strings

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

See Also

Requesting Authorization

Requesting Always Authorization

Request authorization to use location services at any time.