Can't get updated location using custom class

47 views Asked by At

I create a custom LocationManager so I can just use that object to get the location of the user anywhere in the app, but It seems like I can't get the location using the object that I created, here's my LocationManager.swift file

import CoreLocation

class LocationManager: NSObject {
    private var locationManager: CLLocationManager?
    private var locationCallback: ((CLLocation?, String?) -> Void)?
    
    override init() {
        super.init()
        print("initlocmen")
        self.locationManager = CLLocationManager()
        self.locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        self.locationManager?.delegate = self
    }
    
    func requestLocationAndCity(callback: @escaping (CLLocation?, String?) -> Void) {
        self.locationCallback = callback
        
        guard let locationManager = locationManager else {
            return
        }
        
        if CLLocationManager.locationServicesEnabled() {
            let status = CLLocationManager.authorizationStatus()
            
            if status == .authorizedAlways || status == .authorizedWhenInUse {
//                locationManager.requestLocation()
                print("Location allowed")
                locationManager.startUpdatingLocation()
            } else if status == .notDetermined {
                locationManager.requestWhenInUseAuthorization()
            } else if status == .denied || status == .restricted {
                callback(nil, nil)
            } else {
                callback(nil, nil)
            }
        } else {
            callback(nil, nil)
        }
    }
}

extension LocationManager: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationManager?.stopUpdatingLocation()
        print("didUpdateLocations")
        if let location = locations.last {
            // Reverse geocode the location to get the city
            CLGeocoder().reverseGeocodeLocation(location) { [weak self] placemarks, error in
                if let city = placemarks?.first?.locality {
                    self?.locationCallback?(location, city)
                } else {
                    self?.locationCallback?(location, nil)
                }
            }
        } else {
            locationCallback?(nil, nil)
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error getting location:", error)
        locationCallback?(nil, nil)
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse || status == .authorizedAlways {
            locationManager?.requestLocation()
        } else {
            locationCallback?(nil, nil)
        }
    }
}

I use it in my view controller like this

let locationManager = LocationManager()
locationManager.requestLocationAndCity { location, city in
    print("user city", city ?? "none")
}

I only see the logs Location allowed because I already allowed my device to access the location services, but it seems like I can't get on didUpdateLocations,

I already tried to change the locationManager.startUpdatingLocation() into locationManager.requestLocation() but nothing happened.

What's wrong with my code and how can I fix it?

0

There are 0 answers