I looked everywhere, including an example Apple application . But I can’t find anywhere when I can request a location without having to run the code on the phone. The code below, when used in the "interface controller", returns a "not defined" state. I have verified that the privacy key values are set in my info.plist.
import Foundation import CoreLocation class LocationManager: NSObject, CLLocationManagerDelegate { /// Location manager to request authorization and location updates. let manager = CLLocationManager() /// Flag indicating whether the manager is requesting the user location. var isRequestingLocation = false var workoutLocation: CLLocation? func requestLocation() { guard !isRequestingLocation else { manager.stopUpdatingLocation() isRequestingLocation = false return } let authorizationStatus = CLLocationManager.authorizationStatus() switch authorizationStatus { case .notDetermined: isRequestingLocation = true manager.requestWhenInUseAuthorization() case .authorizedWhenInUse: isRequestingLocation = true manager.requestLocation() case .denied: print("Location Authorization Denied") default: print("Location AUthorization Status Unknown") } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard !locations.isEmpty else { return } DispatchQueue.main.async { let lastLocationCoordinate = locations.last!.coordinate print("Lat = \(lastLocationCoordinate.latitude)") print("Long = \(lastLocationCoordinate.longitude)") self.isRequestingLocation = false } } }
Main application info.plist
<key>NSLocationAlwaysUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationWhenInUseUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string>
See extension info.plist
<key>NSLocationAlwaysUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string> <key>NSLocationWhenInUseUsageDescription</key> <string>We will read your location while performing a workout to create a workout route</string>
source share