Instance Method

requestWhenInUseAuthorization()

Requests the user’s permission to use location services while the app is in use.

Declaration

func requestWhenInUseAuthorization()

Discussion

You must call this method or requestAlwaysAuthorization() before you can receive location-related information. You may call requestWhenInUseAuthorization() whenever the current authorization status is not determined (CLAuthorizationStatus.notDetermined).

This method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app Info.plist file, and the presence of that key is required when calling this method. The user prompt displays the following options, which determine the authorization your app can receive.

Option

Authorization

Allow While Using App

When In Use authorization that does not expire.

Allow Once

Temporary When In Use authorization that expires when the app is no longer in use.

Don't Allow

Denied; no further authorization requests are allowed.

After the user makes a selection and determines the status, the location manager delivers the results to the delegate's locationManager(_:didChangeAuthorization:) method. If the initial authorization status is anything other than CLAuthorizationStatus.notDetermined, this method does nothing and doesn't call the locationManager(_:didChangeAuthorization:) method.

If the user's choice grants When In Use authorization to your app, your app can start any location service and is eligible to receive the results while it's in use. If the user's choice grants temporary When In Use authorization, the authorization expires when the app is no longer in use, reverting to Not Determined status (CLAuthorizationStatus.notDetermined). For more details, see Understanding When an App is in Use.

When your app starts standard location services in the foreground, they continue to run in the background if your app has enabled background location updates in the Capabilities tab of your Xcode project. Attempts to start location updates while your app runs in the background will fail. The system displays a location services indicator in the status bar when your app moves to the background with active location services.

See Also

Requesting Authorization for Location Services

func requestAlwaysAuthorization()

Requests the user’s permission for location services whether or not the app is in use.

enum CLAuthorizationStatus

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

class func authorizationStatus() -> CLAuthorizationStatus

Returns the app’s authorization status for using location services.