azekkani
azekkani

Reputation: 33

CoreLocation not working in Swift, but only on MacOS

This location manager in swift is not receiving location updates for MacOS. For your information:

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)")
    }
}

Upvotes: -2

Views: 58

Answers (1)

azekkani
azekkani

Reputation: 33

I 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

Upvotes: -1

Related Questions