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., NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription) and request permission at runtime.


Getting Location Updates

Use CLLocationManager for one-shot or continuous updates.

Example

Location.swift

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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *