Swift Cheat Sheet for iPhone Location Services Access & Usage

This is a cheat sheet of the code and workflow for iPhone location access and usage, from requesting permission to using the location of the user’s device.

Framework Import

import CoreLocation

Set Location Usage Description in Info.plist

This is a required first step

If you don’t set this, your app simply won’t present the user with the system alert to request access to the location (and, of course, you won’t be getting any location data for your app either).

When you request permission to use the device’s location, a short message will appear in the default iOS system dialog. You customize this message by adding one of the following keys to your Info.plist file:

  • Privacy – Location Always and When In Use Usage Description
  • Privacy – Location Always Usage Description
  • Privacy – Location When In Use Usage Description

For the value of this plist property, type a short string describing what you’re using the location for.

Initialize CLLocationManager and Implement CLLocationManagerDelegate

CLLocationManager is the name of the class that gives you a way to request location services permission. It is also responsible for starting the location tracking process.

It needs a corresponding CLLocationManagerDelegate to be assigned. The location manager instance you initialize will alert you to location services authorization changes (ie, if the user turns off location services permissions for your app) through the one of the methods that your delegate class implements.

The location manager instance will also send you the user’s current location through one of the methods your delegate class implements.

Here is an example of initializing the CLLocationManager instance, and setting its delegate.

There’s also a currentLocation variable that will get continually updated…

Here is an example of how to implement the two CLLocationManagerDelegate methods:

Start Location Services, Check Location Authorization Status, Request Permission

When you first start location services in your app, you will want to immediately check your app’s current authorization status for location services, just in case the user has gone in and disabled location services permission for your app since the last time he/she used it.

If it’s the first time the user has launched your app, the .notDetermined case will get hit. This is where you request permission for the first time.

If the user grants permission, you can use the CLLocationManager instance that your class is using to start updating the user’s location.

If, however, the user denies permission, you can alert them to the fact that your app needs access to his/her location for [insert some good reason here].

Here’s a code snippet showing this in action:

Note: You can test for the .notDetermined case by deleting the app on the device, if it’s already been installed or run on a device from the debugger.

Alert Location Services Access Needed

If location services access has been denied or restricted, you can alert the user and direct them to the Settings app to make the appropriate permissions adjustment:

Note: You can test for this case (.restricted and .denied) by going to the Settings app and turning off location services access for your app, if it’s been previously granted.