I have a map tool app in MacOS which needs request Location permission to show MapUserLocationButton.
During development, the request for permission can pop up for the first run in my mac, but when it comes to the Apple review(Submission ID: 11f52f82-1d54-481a-9eed-880521fda2b3), they never see that.
My mac is Macbook air M2 2022, 14.5 (23F79).
Of course, enable App Sandbox - Location and fill up the Privacy - Location When in Use Usage Description
Code:
//
// LocationManager.swift
//
import MapKit
@Observable
class LocationManager: NSObject {
static let shared = LocationManager()
private let manager = CLLocationManager()
var isReady: Bool = false
var showingAlert = false
// var location: CLLocationCoordinate2D?
var isAuthorized: Bool {
#if os(macOS)
.authorized == manager.authorizationStatus
#else
.authorizedWhenInUse == manager.authorizationStatus
#endif
}
private override init() {
super.init()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
setup()
}
private func setup() {
isReady = isAuthorized
#if os(macOS)
if CLLocationManager.locationServicesEnabled() {
checkStatus()
} else {
showingAlert = true
}
#else
checkStatus()
#endif
}
private func checkStatus() {
switch manager.authorizationStatus {
case .notDetermined:
manager.startUpdatingLocation()
#if os(macOS)
#else
manager.requestWhenInUseAuthorization()
#endif
#if os(macOS)
case .restricted, .denied:
showingAlert = true
case .authorizedAlways, .authorizedWhenInUse:
manager.startUpdatingLocation()
#else
// case .authorizedWhenInUse:
// manager.requestLocation()
#endif
default:
break
}
}
}
extension LocationManager: CLLocationManagerDelegate {
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
isReady = isAuthorized
guard isReady else { return }
// manager.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// location = locations.last?.coordinate
}
}
Of course, enable App Sandbox - Location and fill up the Privacy - Location When in Use Usage Description
NSLocationWhenInUseUsageDescription is not used by macOS, and there's a note in the documentation about that. You're looking for NSLocationUsageDescription instead.
— Ed Ford, DTS Engineer