Этот код ниже работает (Не готов к производству). Реализация CLLocationManagerDelegate
работает нормально, а lastKnownLocation
обновляется соответствующим образом.
Не забудьте установить NSLocationWhenInUseUsageDescription
в вашем Info.plist
class LocationManager: NSObject, CLLocationManagerDelegate, BindableObject {
private let manager: CLLocationManager
var didChange = PassthroughSubject<LocationManager, Never>()
var lastKnownLocation: CLLocation? {
didSet {
didChange.send(self)
}
}
init(manager: CLLocationManager = CLLocationManager()) {
self.manager = manager
super.init()
}
func startUpdating() {
self.manager.delegate = self
self.manager.requestWhenInUseAuthorization()
self.manager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
lastKnownLocation = locations.last
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
manager.startUpdatingLocation()
}
}
}