Довольно просто, когда контроллер вида, над которым я работаю, является начальным контроллером вида, вызываются функции делегата моего менеджера местоположения. Когда я устанавливаю контроллер навигации в качестве начального контроллера представления, функции делегата диспетчера местоположения не называются
Вот код
class ViewController: UIViewController {
@IBOutlet weak var cityNameLabel: UILabel!
@IBOutlet weak var tempLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
let o = "°"
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
checkLocationServices()
}
func setViews(weatherData: WeatherData) {
DispatchQueue.main.async {
self.cityNameLabel.text = weatherData.name
self.tempLabel.text = String(weatherData.main.temp)
self.descriptionLabel.text = weatherData.weather[0].description.capitalized
let date = Date(timeIntervalSince1970: weatherData.dt)
print(date.asTimeString)
}
}
}
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard let location = locationManager.location else {print("❇️♊️>>>\(#file) \(#line): guard let failed<<<"); return}
WeatherController().fetchWeather(long: location.coordinate.longitude, lat: location.coordinate.latitude) { (weatherData) in
guard let weatherData = weatherData else {print("❇️♊️>>>\(#file) \(#line): guard let failed<<<"); return}
self.setViews(weatherData: weatherData)
print(weatherData.name)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
// setup our location manager
setupLocationManager()
checkLocationAuthorization()
} else {
// Show alert letting user know they have to turn this on
}
}
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkLocationAuthorization() {
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
break
case .denied:
// Show alert instructing them how to turn on permissions
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
// Show alert letting them know its restricted
break
case .authorizedAlways:
break
default:
break
}
}
}