Request authorization to use location services at any time.
- Core Location
To configure always authorization for location services, follow these steps (Listing 1 demonstrates steps 3-5):
NSLocationkey and the
When In Use Usage Description
NSLocationkey to your
Always And When In Use Usage Description
Infofile. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the
If your app supports iOS 10 and earlier, add the
NSLocationkey to your
Always Usage Description
Infofile. (Xcode displays this key as "Privacy - Location Always Usage Description" in the
Create and configure your
requestinitially to enable your app's basic location support.
When In Use Authorization()
requestmethod only when the user enables a feature of your app that requires that level of authorization.
Don't request authorization at launch time unless you need the user's location immediately or were launched in the background to receive a location update.
Enable When-In-Use Authorization Before Always Authorization
Listing 1 shows one way to enable your app's location services initially. If your app's authorization status is already determined, you don't need to request authorizationa gain. If your app's authorization status is not determined, try to limit your initial authorization request to when-in-use authorization. You can request always authorization right away if your app requires the corresponding location services, but be prepared for the user to grant only when-in-use authorization or to deny your request altogether.
Escalate the App's Authorization Level
In iOS 11 and later, if you initially requested only when-in-use authorization, you can call the
request method at a later time (as shown in Listing 2) to escalate your app's authorization level. When requesting always authorization, the system displays the usage description string stored in the
NSLocation key of your
Info file. The user must then choose whether to grant the request for always authorization or to leave your app with when-in-use authorization. The system lets your app escalate its authorization level only once, displaying an appropriate interface to the user when you do. Subsequent attempts do not display a system interface and do not change your app's authorization level.
Respond to Changes in Authorization Status
When you request authorization, or when your app's authorization status changes, use the
location method of your delegate object to process the changes. Listing 3 shows an implementation of that method that enables or disables features based on the app's current authorization level.