Article

Responding to Changes in Authorization Status

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

Overview

When you request authorization, or when your app's authorization status changes, use the locationManager(_:didChangeAuthorization:) method of your delegate object to process the changes. Listing 1 shows an implementation of that 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 1

Responding to the app's authorization status

func locationManager(_ manager: CLLocationManager, 
                     didChangeAuthorization status: CLAuthorizationStatus) {   switch status {
      case .restricted, .denied:
         // Disable your app's location features
         disableMyLocationBasedFeatures()
         break
            
      case .authorizedWhenInUse:
         // Enable your app's location features.
         enableMyLocationFeatures()
         break
            
      case .authorizedAlways:
         // Enable or prepare your app's location features that can run any time.
         enableMyAlwaysFeatures()
         break
            
      case .notDetermined:
         break
   }
}

See Also

Authorization

Choosing the Location Services Authorization to Request

Determine the authorization your app needs to access location data.

Requesting Authorization for Location Services

Ask the user for permission to access their location.

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