0Pricing
Swift Academy · Lesson

Getting the User's Location

Receive location updates with CLLocationManager.

Starting Location Updates

Once authorized, call startUpdatingLocation() to begin receiving a continuous stream of fixes. The manager delivers them through the delegate. Always call stopUpdatingLocation() when you no longer need them to save battery.

manager.startUpdatingLocation()
// ... later
manager.stopUpdatingLocation()

Receiving Locations in the Delegate

New fixes arrive in locationManager(_:didUpdateLocations:). The array is ordered oldest-to-newest, so the last element is the most recent location.

func locationManager(_ m: CLLocationManager,
                     didUpdateLocations locations: [CLLocation]) {
    guard let latest = locations.last else { return }
    print(latest.coordinate.latitude, latest.coordinate.longitude)
}

All lessons in this course

  1. Requesting Location Authorization
  2. Getting the User's Location
  3. Displaying Maps in SwiftUI
  4. Geocoding and Regions
← Back to Swift Academy