CLLocationManager: requestWhenInUseAuthorization() method
The requestWhenInUseAuthorization() method requests "while using the app" location from the user.
Syntax
requestWhenInUseAuthorization()Parameters
None.
Return value
A Promise that fulfills with the authorization status after the request, a string.
- When the status is
'notDetermined', the system shows a prompt containingNSLocationWhenInUseUsageDescriptionfromnacre.config.json. After the user chooses, the Promise fulfills with the new status. The prompt's appearance and options are determined by the system. Not verified in the simulator. - When the status is no longer
'notDetermined', it fulfills immediately with the current status, without a prompt. The system asks each app only once. - If called several times at once, the prompt appears only once and all the Promises fulfill together.
When the Promise is rejected, you get a CLLocationError:
name | When |
|---|---|
'InvalidStateError' | Info.plist has no NSLocationWhenInUseUsageDescription. This doesn't happen with projects generated by Nacre's command-line tool, which checks it at generation time |
'InvalidStateError' | The app is in the background. The system asks only while the user is using the app |
'CLError' | The system reports that the prompt wasn't shown to the user. Not verified in the simulator |
The message of the two 'InvalidStateError' cases:
requestWhenInUseAuthorization: NSLocationWhenInUseUsageDescription is missing from Info.plist, so the system will not ask; add it under ios.infoPlist in nacre.config.jsonrequestWhenInUseAuthorization: the app is in the background, and the system only asks while the app is in use
Exceptions
None. All errors come through Promise rejection.
Description
- It's safe to call it every time before using the location: once asked, it doesn't prompt again.
- After the user denies, the app has no way to show the prompt again; only the user can change it in the system Settings app.
- The user can choose "Allow Once", in which case it fulfills with
'authorizedWhenInUse'. Not verified in the simulator.
Examples
js
const manager = new CLLocationManager();
manager.requestWhenInUseAuthorization().then((status) => {
console.log(status);
});Effect: when the status is already determined, prints the current status immediately. When the status is 'notDetermined', the system shows a prompt and the new status is printed after the user chooses (not verified in the simulator).
Corresponding iOS API
-[CLLocationManager requestWhenInUseAuthorization]. In ObjC the result comes from the delegate's locationManagerDidChangeAuthorization:; here it comes from the returned Promise.
See also
authorizationStatus: the five statusesauthorizationchange: notification that the status changed- Getting the user's location: when to ask for the best result
- Installing
@nacrejs/core-location