I am encountering some issues with location services in macOS apps. Despite including the necessary keys in the app’s info.plist, I am unable to request user location permission. Here are the details:
Cannot request location permission using requestWhenInUseAuthorization(), ie., no location permission grant permissions popup is appearing.
The app contains NSLocationUsageDescription in the info.plist. I have also tested by adding NSLocationAlwaysAndWhenInUseUsageDescription, NSLocationWhenInUseUsageDescription, and NSLocationAlwaysUsageDescription, but it didn't work.
So to show the location request popup, I was directly(irrespective of the authorizationStatus) requesting location using requestLocation(), which showed the location permission grant permissions popup. But then I encountered another issue. If the popup is left as it is, every time requestLocation() is called a new location request popup appears (which comes after allowing/denying the popup). Also observed that didChangeAuthorization is only called on allowing/denying the last location request pop up.
Initial calls to CLLocationManager().authorizationStatus return Not Determined.
When I checked the location permission state of my app on launch, I used to get not Determined for some time, even if the permission was granted.
Code snippet:
private var locationManager: CLLocationManager {
let lm = CLLocationManager()
lm.delegate = self
lm.desiredAccuracy = kCLLocationAccuracyBest
return lm
}
private var currentAuthorizationStatus: CLAuthorizationStatus {
if #available(macOS 11.0, *) {
return CLLocationManager().authorizationStatus
} else {
return CLLocationManager.authorizationStatus()
}
}
switch currentAuthorizationStatus {
case .notDetermined:
print("requesting permission")
locationManager.requestWhenInUseAuthorization()
case .restricted, .denied:
print("location permission is restricted")
case .authorizedAlways, .authorizedWhenInUse, .authorized:
print("requesting location")
locationManager.requestLocation()
@unknown default:
print("Unknown error occurred")
}
I would appreciate any guidance or suggestions on how to resolve these issues. Thank you!