The CoreLocation framework provides your app with the ability to determine the device's geographic location, altitude, and orientation.
Whether you are building a fitness tracker, a delivery app, or a simple weather widget, CoreLocation is the foundation of spatial awareness in iOS.
Location data is highly sensitive. Before fetching coordinates, you must request permission using CLLocationManager.
You also need to add keys like NSLocationWhenInUseUsageDescription to your Info.plist.
Apple heavily encourages developers to use "When In Use" authorization, meaning you can only access the location while the app is actively on the screen.
import CoreLocationclass LocationHelper: NSObject, CLLocationManagerDelegate { let manager = CLLocationManager() override init() { super.init() manager.delegate = self // Request permission from the user manager.requestWhenInUseAuthorization() } }
Once authorized, you can tell the manager to start updating the location.
The manager will continuously ping the GPS hardware and deliver the coordinates to your class via the delegate methods.
import CoreLocationclass LocationHelper: NSObject, CLLocationManagerDelegate { let manager = CLLocationManager() // ... initialization code from above ... func startTracking() { manager.desiredAccuracy = kCLLocationAccuracyBest manager.startUpdatingLocation() } // This delegate function fires whenever a new location is found func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let latestLocation = locations.last else { return } let latitude = latestLocation.coordinate.latitude let longitude = latestLocation.coordinate.longitude print("Currently at: \(latitude), \(longitude)") // Stop updating if you only need it once to save battery! manager.stopUpdatingLocation() } }
CoreLocation also supports Region Monitoring, commonly known as Geofencing.
You can define a circular region on a map using a coordinate and a radius.
CoreLocation will wake your app up in the background and notify you the exact moment the user physically enters or exits that circle!
This is incredibly powerful for retail apps sending nearby coupons, or smart home apps turning on the lights as you arrive.
If your app needs location updates all day long (like a mileage tracker), continuous GPS usage will drain the battery in hours.
Instead, you can use the Significant Location Change service, which relies on cell towers and Wi-Fi networks to deliver incredibly low-power updates as the user travels long distances.
Which CoreLocation feature allows you to trigger an event when a user enters a specific geographic circle?