Short answer

Add NSLocationWhenInUseUsageDescription to Info.plist, set a delegate on a CLLocationManager, call requestWhenInUseAuthorization(), and wait for locationManagerDidChangeAuthorization(_:) before calling requestLocation(). The coordinates arrive in locationManager(_:didUpdateLocations:). The 2018 code read manager.location right after asking: on a first launch the status check skipped the read, and on later launches the property can still be nil, which crashes the implicitly unwrapped CLLocation! it was stored in.

SWIFT TIPS · UIKIT · CORE LOCATION

First posted on July 21, 2018 for Swift 4, as "How to cek current longitude and latitude Swift 4". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.

In 2018 I needed the user's latitude and longitude. The post added the usage description keys, asked for when-in-use permission in viewDidLoad, and if the status was authorized, read locManager.location into an implicitly unwrapped CLLocation! and printed its coordinates.

That works on the second launch, when permission was already granted and the system has a recent location. On the first launch it has a problem the rewrite fixes.

Getting one location, step by step

import UIKit
import CoreLocation

/// Asks for permission once, then gets a single location fix and reports it.
final class CurrentLocationViewController: UIViewController, @preconcurrency CLLocationManagerDelegate {
    @IBOutlet private weak var coordinateLabel: UILabel!
    private let manager = CLLocationManager()
    private var wantsLocation = false   // only fetch after the user asks

    override func viewDidLoad() {
        super.viewDidLoad()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyHundredMeters   // enough for "near me"
    }

    @IBAction func findMeTapped(_ sender: UIButton) {
        wantsLocation = true
        switch manager.authorizationStatus {
        case .notDetermined:
            manager.requestWhenInUseAuthorization()   // the answer arrives in the delegate
        case .authorizedWhenInUse, .authorizedAlways:
            manager.requestLocation()
        default:
            wantsLocation = false
            coordinateLabel.text = "Location is off for this app. You can turn it on in Settings."
        }
    }

    // Called when the manager is created and whenever permission changes
    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        guard wantsLocation else { return }   // also runs at creation, before any tap
        if manager.authorizationStatus == .authorizedWhenInUse || manager.authorizationStatus == .authorizedAlways {
            manager.requestLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.last else { return }
        wantsLocation = false
        let c = location.coordinate
        coordinateLabel.text = String(format: "%.5f, %.5f (within %.0f m)", c.latitude, c.longitude, location.horizontalAccuracy)
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        wantsLocation = false
        coordinateLabel.text = "Couldn't get your location right now."
    }
}

// iOS 17 and later: an async stream of updates instead of a delegate
@available(iOS 17.0, *)
func firstAccurateLocation() async throws -> CLLocation? {
    for try await update in CLLocationUpdate.liveUpdates() {
        if let location = update.location, location.horizontalAccuracy <= 100 {
            return location
        }
    }
    return nil
}

Why the order matters

Location permission and location fixes are both asynchronous. requestWhenInUseAuthorization() shows a dialog and returns immediately, so in the 2018 code the status check on the next line still saw "not determined" on a first launch and skipped the read. On later launches it read manager.location, and Apple's documentation says that property is nil if no location data has ever been retrieved. Assigning that to a CLLocation! and reading .coordinate stops the app. I didn't run the 2018 code to see it crash; this follows from the documentation.

The version above reacts instead of checking once:

  • The button decides. Not asked yet: ask. Allowed: request a location. Denied or restricted: explain where to turn it on.
  • The delegate continues. Apple says locationManagerDidChangeAuthorization(_:) is called when the manager is created and whenever the status changes. Because of that first call, the code keeps a wantsLocation flag set by the button: without it, a user who allowed location last week would have their location fetched as soon as the screen opens. With the flag, tapping Allow in the permission dialog continues straight to requestLocation(), and nothing happens until the user asks.
  • One fix, then stop. requestLocation() delivers a single location to didUpdateLocations and then stops location services, according to Apple's documentation, or calls didFailWithError if it can't get one. That suits "where am I right now" far better than starting continuous updates and forgetting to stop them.
  • Ask for the accuracy you need. A hundred meters is plenty for nearby results and arrives faster than the best accuracy setting.

The label shows the coordinates together with horizontalAccuracy, so you can see how precise the fix was.

Swift 6 and the delegate

My first version declared a plain CLLocationManagerDelegate conformance on the view controller. In Swift 6 mode it failed:

o044_v1.swift:5:62: error: conformance of 'CurrentLocationViewController' to protocol 'CLLocationManagerDelegate' crosses into main actor-isolated code and can cause data races

The view controller lives on the main actor, while the protocol's methods aren't marked that way. Apple's documentation says Core Location calls the delegate on the run loop of the thread where you created the manager, which here is the main thread, so marking the conformance @preconcurrency is accurate: it tells the compiler to trust that and check it at run time. In Swift 5 mode the compiler warns that @preconcurrency has no effect there, which is harmless.

iOS 17 and later: async updates

firstAccurateLocation() shows the newer API. CLLocationUpdate.liveUpdates() is an async sequence of updates, and the function returns the first location accurate to 100 meters. It still needs the same permission and Info.plist key. With an iOS 15 deployment target it sits behind @available(iOS 17.0, *).

A quick check for your project

  1. Search for .location!, CLLocation! and manager.location?.coordinate read right after a permission request.
  2. Search for CLLocationManager.authorizationStatus(). The class method is deprecated since iOS 14. Use the instance property authorizationStatus.
  3. Delete the app, install it again, and tap your location button on the very first launch.

What the 2018 version said

The original listed NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription, imported MapKit, and printed the latitude and longitude from locManager.location. Type-checked with Xcode 26.2, it still compiles, with a deprecation warning for each use of the class method authorizationStatus(). The "always" key it mentions, NSLocationAlwaysUsageDescription, is deprecated: Apple's documentation says apps targeting iOS 11 and later use NSLocationAlwaysAndWhenInUseUsageDescription instead. Ask for always-on location only if your app really needs it in the background.

Sources