Skip to content

Instantly share code, notes, and snippets.

@NarekChang
Last active February 19, 2022 12:33
Show Gist options
  • Save NarekChang/9a21e66f51a9581b61a20e72ba4530d1 to your computer and use it in GitHub Desktop.
Save NarekChang/9a21e66f51a9581b61a20e72ba4530d1 to your computer and use it in GitHub Desktop.
import Foundation
import CoreLocation
import Combine
final class LocationManager: NSObject, CLLocationManagerDelegate {
// Dependencies
private lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
return manager
}()
// Properties
public var didUpdateStatus: ((CLAuthorizationStatus) -> Void)?
public var didUpdateLocation: ((CLLocation) -> Void)?
// MARK: - Public
func startUpdatingLocation() {
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
didUpdateStatus?(status)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
didUpdateLocation?(location)
// без этой строчки manager.didUpdateLocation = { ... } может вызываться несколько раз, если надо чтобы перестал - раскомментить
// didUpdateLocation = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment