This location manager in swift is not receiving location updates for MacOS. For your information:
- This same file is shared with the iOS instance of the app and works fine there.
- This is not my device only, as the app was rejected by App Review because of the location not being updated.
- I do have the necessary privacy tags in
Info.plist
- I tested the same code in
AppDelegate
with the same result - I am getting
0
for the location status, which means `.notDetermined' - This has been tested on a fresh install and I gave the app the necessary permissions in settings.
import SwiftUI
import CoreLocation
import Combine
import MapKit
import BackgroundTasks
/// Manages location-related services and permissions for the app.
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
/// Instance of `CLLocationManager` to handle location updates.
private let locationManager = CLLocationManager()
/// Published property to monitor the current authorization status.
@Published var locationStatus: CLAuthorizationStatus?
/// Published property to store the user's current location.
@Published var location: CLLocation?
/// Initializes the location manager, sets up permissions, and starts updating location.
override init() {
super.init()
guard CLLocationManager.locationServicesEnabled() else {
print("Location services are not enabled.")
return
}
self.locationStatus = locationManager.authorizationStatus
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if self.locationStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
locationManager.startUpdatingLocation()
}
/// Provides a string representation of the current authorization status.
var statusString: String {
guard let status = locationStatus else {
return "unknown"
}
switch status {
case .notDetermined: return "notDetermined"
case .authorizedWhenInUse: return "authorizedWhenInUse"
case .authorizedAlways: return "authorizedAlways"
case .restricted: return "restricted"
case .denied: return "denied"
default: return "unknown"
}
}
/// Handles changes in the location authorization status.
/// - Parameters:
/// - manager: The `CLLocationManager` instance reporting the status change.
/// - status: The updated authorization status.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.locationStatus = status
print("Authorization status changed: \(status.rawValue)")
#if os(tvOS)
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
#endif
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus
self.locationStatus = status
print("Authorization status changed: \(status.rawValue)")
if status == .authorized || status == .authorizedAlways || status == .authorized {
locationManager.startUpdatingLocation()
}
}
/// Updates the user's current location.
/// - Parameters:
/// - manager: The `CLLocationManager` instance providing the location update.
/// - locations: An array of updated location objects.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if self.location == nil {
self.location = location
}
}
/// Handles errors encountered by the location manager.
/// - Parameters:
/// - manager: The `CLLocationManager` instance reporting the error.
/// - error: The error encountered by the location manager.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location manager failed with error: \(error.localizedDescription)")
}
}
This location manager in swift is not receiving location updates for MacOS. For your information:
- This same file is shared with the iOS instance of the app and works fine there.
- This is not my device only, as the app was rejected by App Review because of the location not being updated.
- I do have the necessary privacy tags in
Info.plist
- I tested the same code in
AppDelegate
with the same result - I am getting
0
for the location status, which means `.notDetermined' - This has been tested on a fresh install and I gave the app the necessary permissions in settings.
import SwiftUI
import CoreLocation
import Combine
import MapKit
import BackgroundTasks
/// Manages location-related services and permissions for the app.
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
/// Instance of `CLLocationManager` to handle location updates.
private let locationManager = CLLocationManager()
/// Published property to monitor the current authorization status.
@Published var locationStatus: CLAuthorizationStatus?
/// Published property to store the user's current location.
@Published var location: CLLocation?
/// Initializes the location manager, sets up permissions, and starts updating location.
override init() {
super.init()
guard CLLocationManager.locationServicesEnabled() else {
print("Location services are not enabled.")
return
}
self.locationStatus = locationManager.authorizationStatus
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if self.locationStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
}
locationManager.startUpdatingLocation()
}
/// Provides a string representation of the current authorization status.
var statusString: String {
guard let status = locationStatus else {
return "unknown"
}
switch status {
case .notDetermined: return "notDetermined"
case .authorizedWhenInUse: return "authorizedWhenInUse"
case .authorizedAlways: return "authorizedAlways"
case .restricted: return "restricted"
case .denied: return "denied"
default: return "unknown"
}
}
/// Handles changes in the location authorization status.
/// - Parameters:
/// - manager: The `CLLocationManager` instance reporting the status change.
/// - status: The updated authorization status.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
self.locationStatus = status
print("Authorization status changed: \(status.rawValue)")
#if os(tvOS)
if status == .authorizedWhenInUse {
locationManager.requestLocation()
}
#endif
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus
self.locationStatus = status
print("Authorization status changed: \(status.rawValue)")
if status == .authorized || status == .authorizedAlways || status == .authorized {
locationManager.startUpdatingLocation()
}
}
/// Updates the user's current location.
/// - Parameters:
/// - manager: The `CLLocationManager` instance providing the location update.
/// - locations: An array of updated location objects.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
if self.location == nil {
self.location = location
}
}
/// Handles errors encountered by the location manager.
/// - Parameters:
/// - manager: The `CLLocationManager` instance reporting the error.
/// - error: The error encountered by the location manager.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location manager failed with error: \(error.localizedDescription)")
}
}
Share
Improve this question
asked Jan 19 at 19:53
azekkaniazekkani
331 silver badge6 bronze badges
14
|
Show 9 more comments
1 Answer
Reset to default -1I was able to fix this problem by removing the Hardened Runtime
Capability for MacOS. This oddly fixed the problem and allowed my app to get location updates again. Huge Thanks to @Paulw11
CLLocationManager
. – HangarRash Commented Jan 19 at 19:57requestWhenInUseAuthorization
andrequestAlwaysAuthorization
at the same time (either one, or the other). You shouldn't callstartUpdatingLocation
immediately after that (authorization is async call). You are calling it frominit
, so you have to make sure thisLocationManager
is a singleton, and is only called when there's already some UI to display the authorization form over. Also @HangarRash provided a link to a good answer. What exactly didn't work for you when you "tried that code"? – timbre timbre Commented Jan 19 at 21:080
or.notDetermined
. – azekkani Commented Jan 19 at 21:47