Core Location
Core Location
Request authorization and read user location updates with CLLocationManager, respecting privacy and power.
Authorization
Add usage descriptions in Info.plist (e.g., NSLocationWhenInUseUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription) and request permission at runtime.
Getting Location Updates
Use CLLocationManager for one-shot or continuous updates.
Example
import CoreLocation
class LocationDelegate: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
override init() {
super.init()
manager.delegate = self
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Last: \(locations.last?.coordinate ?? .init())")
}
}
This example configures a CLLocationManager, requests authorization, and prints the last known coordinate when updates arrive.
Tip: Optimize for power: choose appropriate accuracy, stop updates when not needed, and consider visit or significant-change monitoring.