I would like to know what is the best way to deal with this situation. I have two different view controllers, both will use the same didUpdateLocations method from CLlocationManagerDelegate. I'm using an extensions for both of them and conform them to the CLManagerDelegate. I'm wondering if there is another way for the same result. Thanks for all explanation and replies.
First view controller
extension FirstViewController: CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
}
}
}
Second view controller
extension SecondViewController: CLLocationManagerDelegate{
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
}
}
}
I was thinking to the following code, but I do not know if it better than the previous and do the same thing for the second view controller.
protocol localisation {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
}
extension FirstViewController: localisation, CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.stopUpdatingLocation()
print("\(location.coordinate.longitude), \(location.coordinate.latitude)")
}
}