【问题标题】:ViewController doesn't load data after granting location permissionViewController 在授予位置权限后不加载数据
【发布时间】:2017-02-18 14:13:38
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift swift3 cllocationmanager


    【解决方案1】:

    你可以用这个来处理错误情况:

    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)
    
    
    }
    

    【讨论】:

      【解决方案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
      }
      

      【讨论】:

      • 所以我进行了编辑,现在我没有任何数据下载到界面。在 didUpdateLocations ...我输入了downloadWeatherDetails,但没有运气。
      • 请在didUpdateLocations 中设置一个brteakpoint,看看它是否被触发。如果没有,您可能忘记将委托设置为locationManager
      • 我们说话的时候我正在穿上跑步。但是,我看到我已经在 case 语句之前设置了locationManager.delegate = self
      猜你喜欢
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 2014-08-16
      相关资源
      最近更新 更多