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

时间:2017-02-18 14:13:39

标签: ios swift swift3 cllocationmanager

我遇到一个奇怪的问题,只发生在我的应用的第一次初始加载。应用首次加载后,我会要求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()
        }
    }
}

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

2 个答案:

答案 0 :(得分:3)

你可以用以下方法处理错误案例:

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)


}

答案 1 :(得分:2)

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
}