ViewController 授予位置权限后不加载数据

ViewController doesn't load data after granting location permission

我遇到了一个奇怪的问题,它只在我的应用程序首次初始加载时发生。应用程序首次加载后,我要求 requestWhenInUseAuthorization()。发生这种情况后,即使我调用了正确的函数,数据也不会加载到视图上。

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()

    //Get UserDefault
    let value = userDefaults.string(forKey: "Unit")

    if value == nil {
        userDefaults.set( "F", forKey: "Unit")
    }

    //Location Manager Setup
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()

    tableView.delegate = self
    tableView.dataSource = self

    currentWeather = CurrentWeather()

}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    locationAuthStatus()
}

locationAuthStatus()

func locationAuthStatus() {

    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        currentLocation = locationManager.location
        Location.sharedInstance.latitude = currentLocation.coordinate.latitude
        Location.sharedInstance.longitude = currentLocation.coordinate.longitude

        downloadWeatherDetails()
    }
}

func downloadWeatherDetails() {
    currentWeather.downloadWeatherDetails {
        self.downloadForecastData {
            self.updateMainUI()
        }
    }
}

一旦我进入设置视图控制器,进行更改,然后返回主视图,数据就会加载。关于如何处理这个的任何建议?

requestWhenInUseAuthorization 异步运行,因此您要求在您的应用获得许可之前监控位置。最重要的是,无法保证您的应用程序会在 viewDidAppear 被调用时收到位置信息,因此请不要在那里下载您的天气数据。

试试这个:

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse, .authorizedAlways:
        self.startMonitoringLocation()
    case .notDetermined:
        locationManager.requestWhenInUseAuthorization()
    default:
        print("permission denied")
        break;
    }

    // other things...
}

func startMonitoringLocation() {
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startMonitoringSignificantLocationChanges()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    // The user has given permission to your app
    if status == .authorizedWhenInUse || status == .authorizedAlways {
        self.startMonitoringLocation()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // download weather data here
}

你可以这样处理错误情况:

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

let alertController = UIAlertController(title: "No Location", message: "No Location", preferredStyle: .alert)
    let defaultAction = UIAlertAction(title: "EXIT", style: .default) { UIAlertAction in exit(0)
    }
    alertController.addAction(defaultAction)
    present(alertController, animated: true, completion: nil)


}