Requesting Location Authorization
Handle permissions and privacy correctly.
Requesting Location Authorization is a free Swift Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Location Needs Permission
On Apple platforms, accessing the user's location is a privacy-sensitive operation. Your app must explicitly request authorization before Core Location will deliver any coordinates. The system shows a permission dialog the first time you ask.
- Authorization is granted per-app by the user.
- You cannot bypass it programmatically.
import CoreLocation
let manager = CLLocationManager()Info.plist Usage Description Keys
Before requesting authorization you must add a usage description string to Info.plist. These strings are shown to the user in the permission dialog. The two key ones are:
NSLocationWhenInUseUsageDescription— for foreground-only access.NSLocationAlwaysAndWhenInUseUsageDescription— for background access too.
Omitting the relevant key causes the request to silently fail or crash.
<!-- Info.plist -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>We use your location to show nearby places.</string>Creating the CLLocationManager
CLLocationManager is the central object that coordinates location services. You create one instance, keep a strong reference to it, and assign a delegate to receive callbacks.
final class LocationService: NSObject {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
}
}Requesting When-In-Use Authorization
Call requestWhenInUseAuthorization() to ask for foreground access. The system presents the dialog only once; after that the choice is remembered. If already determined, the call does nothing.
func requestPermission() {
manager.requestWhenInUseAuthorization()
}Requesting Always Authorization
For background updates (geofencing, continuous tracking) you call requestAlwaysAuthorization(). Apple requires that you first obtain When-In-Use, then escalate to Always. Use it sparingly and justify it in your usage description.
func requestAlways() {
manager.requestAlwaysAuthorization()
}Reading the Authorization Status
The current permission state lives in manager.authorizationStatus (an instance property on iOS 14+). Possible values include .notDetermined, .denied, .restricted, .authorizedWhenInUse, and .authorizedAlways.
let status = manager.authorizationStatus
switch status {
case .notDetermined: print("Ask the user")
case .denied, .restricted: print("No access")
case .authorizedWhenInUse, .authorizedAlways: print("Good to go")
@unknown default: break
}The CLLocationManagerDelegate
Authorization changes are delivered asynchronously through the delegate. Implement locationManagerDidChangeAuthorization(_:) to react when the user makes a choice. Never assume permission is granted immediately after calling request.
extension LocationService: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse, .authorizedAlways:
manager.startUpdatingLocation()
default:
print("Not authorized")
}
}
}Handling Denied or Restricted
If the user denies access, you cannot re-prompt. Instead, detect .denied and guide them to Settings. .restricted means access is blocked by parental controls or an MDM profile and is also non-recoverable in-app.
func handle(_ status: CLAuthorizationStatus) {
if status == .denied {
// Deep-link the user to Settings > Privacy > Location
print("Open Settings to enable location")
}
}Accuracy Authorization (Precise vs Reduced)
Since iOS 14 users can grant only reduced (approximate) accuracy. Check manager.accuracyAuthorization. If you truly need precise data (e.g. turn-by-turn), call requestTemporaryFullAccuracyAuthorization(withPurposeKey:) with a key defined in Info.plist.
if manager.accuracyAuthorization == .reducedAccuracy {
manager.requestTemporaryFullAccuracyAuthorization(
withPurposeKey: "PreciseForNavigation"
)
}Checking Location Services Are Enabled
Beyond per-app permission, the device-wide Location Services toggle can be off. Check CLLocationManager.locationServicesEnabled() on a background queue (calling it on the main thread can block). If disabled, no app receives location.
DispatchQueue.global().async {
if CLLocationManager.locationServicesEnabled() {
print("System location is on")
} else {
print("System location is off")
}
}A Complete Authorization Flow
Putting it together: create the manager, set the delegate, request When-In-Use, then start updates inside the authorization callback. This is the canonical pattern for any location-aware feature.
final class LocationService: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.requestWhenInUseAuthorization()
}
func locationManagerDidChangeAuthorization(_ m: CLLocationManager) {
if m.authorizationStatus == .authorizedWhenInUse {
m.startUpdatingLocation()
}
}
}Quick Check: Authorization
Test your understanding of the authorization flow.
Recap: Requesting Authorization
You learned how to safely request location permission:
- Add
NSLocationWhenInUseUsageDescription(and Always variant if needed) to Info.plist. - Create a
CLLocationManager, keep a strong reference, set its delegate. - Call
requestWhenInUseAuthorization(), then escalate to Always only if required. - React in
locationManagerDidChangeAuthorization(_:)and handle denied/restricted/reduced-accuracy cases.
Frequently asked questions
Is the “Requesting Location Authorization” lesson free?
Yes — the full text of “Requesting Location Authorization” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Requesting Location Authorization”?
Handle permissions and privacy correctly. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Requesting Location Authorization” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Requesting Location Authorization
- Getting the User's Location
- Displaying Maps in SwiftUI
- Geocoding and Regions