Adding something else to this thread. Here a sample code that I use in my original code (not exactly this way, but the main part is quite close)
I started a new Command Line Tool with Xcode 14.2, i notice that there's no more config.plist so I take from my app to ensure that NSLocationAlwaysUsageDescription, NSLocationUsageDescription and NSLocationWhenInUseUsageDescription are there - config.plist attached here too. No luck with Ventura so far.
import Foundation
import CoreLocation
struct TCoordinates {
var latitude: Double = 0
var longitude: Double = 0
var altitude: Double = 0
var course: Double = 0
var precision: Double = 0
}
let locationManager = CLLocationManager()
var coordinates: TCoordinates
coordinates = TCoordinates.init()
func getCoordinates(locationManager: CLLocationManager) -> TCoordinates {
var ret = TCoordinates.init()
ret.latitude = locationManager.location?.coordinate.latitude ?? 0
ret.longitude = locationManager.location?.coordinate.longitude ?? 0
ret.altitude = locationManager.location?.altitude ?? 0
ret.course = locationManager.location?.course ?? 0
ret.precision = locationManager.location?.horizontalAccuracy ?? 0
return ret
}
private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
// If status has not yet been determied, ask for authorization
manager.requestWhenInUseAuthorization()
break
case .authorizedWhenInUse:
// If authorized when in use
manager.startUpdatingLocation()
break
case .authorizedAlways:
// If always authorized
manager.startUpdatingLocation()
break
case .restricted:
// If restricted by e.g. parental controls. User can't enable Location Services
break
case .denied:
// If user denied your app access to Location Services, but can grant access from Settings.app
break
default:
break
}
}
func getGeoLocation() {
coordinates = getCoordinates(locationManager: locationManager)
if CLLocationManager.locationServicesEnabled() {
let authorizationStatus: CLAuthorizationStatus
if #available(iOS 14, *) {
authorizationStatus = locationManager.authorizationStatus
} else {
authorizationStatus = CLLocationManager.authorizationStatus()
}
switch authorizationStatus {
case .restricted, .notDetermined:
locationManager.requestAlwaysAuthorization()
print("Restricted")
exit(0)
case .denied:
print("Denied")
case .authorizedAlways, .authorizedWhenInUse:
print("Authorized")
case .authorized:
print("Authorized")
@unknown default:
print("Unavailable")
}
}
}
/* -------- */
locationManager.startUpdatingLocation()
getGeoLocation()
autoreleasepool {
RunLoop.main.run()
}