Last active
February 19, 2022 12:33
-
-
Save NarekChang/9a21e66f51a9581b61a20e72ba4530d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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