Location Quickstart - codepath/ios_guides GitHub Wiki
Location
Step 1: Add the Location Framework
In build phases, add the location framework.
Step 2: Identifying the desired location permission
So the first thing you need to do is to add the appropriate keys to your Info.plist file:
NSLocationWhenInUseUsageDescription— required if you callrequestWhenInUseAuthorization().NSLocationAlwaysAndWhenInUseUsageDescription— required if you callrequestAlwaysAuthorization()on iOS 11+ (the olderNSLocationAlwaysUsageDescriptionis deprecated).
Note: requesting "Always" authorization on iOS 11 and later requires both keys to be present in Info.plist. If NSLocationWhenInUseUsageDescription is missing, requestAlwaysAuthorization() will silently fail and no permission prompt will be shown. Since Step 4 below calls requestAlwaysAuthorization(), add both keys.
Step 3: Create the location manager
In the app delegate,
import CoreLocation
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var locationManager: CLLocationManager! = CLLocationManager()
Step 4: Request permission
locationManager.requestAlwaysAuthorization()
Step 5: Start Updating Location
locationManager.startUpdatingLocation()
Step 6: Implement Location Delegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else { return }
print(location)
}