I include some basic code to better illustrate how the CLLocationManager is configured and used.
class LocationDataManager: NSObject, CLLocationManagerDelegate {
private let locationManager: CLLocationManager
var activityType: CLActivityType = .automotiveNavigation {
didSet {
locationManager.activityType = activityType
}
}
var desiredAccuracy: CLLocationAccuracy = kCLLocationAccuracyBestForNavigation {
didSet {
locationManager.desiredAccuracy = desiredAccuracy
}
}
var pausesLocationUpdatesAutomatically: Bool = false {
didSet {
locationManager.pausesLocationUpdatesAutomatically = pausesLocationUpdatesAutomatically
}
}
var distanceFilter: CLLocationDistance = kCLDistanceFilterNone {
didSet {
locationManager.distanceFilter = distanceFilter
}
}
init() {
locationManager = CLLocationManager()
locationManager.activityType = .automotiveNavigation // controlled by the Picker
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation // controlled by the Picker
locationManager.pausesLocationUpdatesAutomatically = false // controlled by the Toggle
locationManager.distanceFilter = kCLDistanceFilterNone // controlled by the Picker
locationManager.allowsBackgroundLocationUpdates = true
}
func start() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func stop() {
locationManager.stopUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations.last)
}
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
print(#function)
}
func locationManagerDidResumeLocationUpdates(_ manager: CLLocationManager) {
print(#function)
}
}
Topic:
App & System Services
SubTopic:
Core OS
Tags: