Request authorization to use location services only when your app is running.
- Core Location
To configure when-in-use authorization for location services, follow these steps (Listing 1 demonstrates steps 2 and 3.):
NSLocationkey to your app's Information Property List file. (Xcode displays this key as "Privacy - Location When In Use Usage Description" in the property list editor.)
When In Use Usage Description
Create and configure your
requestmethod of your
When In Use Authorization()
CLLocationobject prior to starting any location services.
Request authorization at the point where you need location services. For example, wait until the user activates a feature of your app that requires location data. Don't request authorization at launch time unless you need the user's location immediately or your app was launched in the background by the system to handle a location update.
Request When-In-Use Authorization
Listing 1 shows how you might request when-in-use authorization before starting your app's location-related services, by calling the
request method. You need to request authorization only when your app's authorization status is not determined. If your app's authorization status is already determined, you can simply start the services that you need.
Respond to Changes in Authorization Status
When you make an authorization request, Core Location processes your request asynchronously and delivers the results to the
location method of your location manager's delegate object. Use that method to determine whether the user granted or denied your request, and to take any appropriate actions in response.
Listing 2 shows an implementation of the
location 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.