Instance Method


Requests permission to use location services while the app is in the foreground.


func requestWhenInUseAuthorization()


When the current authorization status is 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’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager(_:didChangeAuthorization:) method. If the current authorization status is anything other than CLAuthorizationStatus.notDetermined, this method does nothing and does not call the locationManager(_:didChangeAuthorization:) method.

You must call this method or the requestAlwaysAuthorization() method prior to using location services. If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.) When started in the foreground, services 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 services while your app is running 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.

For more information about the NSLocationWhenInUseUsageDescription key, see Information Property List Key Reference.

See Also

Requesting Authorization for Location Services

func requestAlwaysAuthorization()

Requests permission to use location services whenever the app is running.

enum CLAuthorizationStatus

Constants indicating whether the app is authorized to use location services.