Location manager didUpdateLocations not being called

こ雲淡風輕ζ 提交于 2020-01-22 00:41:46

问题


For some reason didUpdateLocations is not being called, even when I set the delegate to the view controller. In info.plist I set the key Privacy - Location When In Use Usage Description to a description. So I'm not sure what I could be doing wrong?

import MapKit
import CoreLocation

class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate {
    override func viewDidLoad() {
          //Ask user for location
        locationManager = CLLocationManager()
        locationManager.requestWhenInUseAuthorization()

        //Use users current location if no starting point set
        if CLLocationManager.locationServicesEnabled() {
            let locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        locationManager = CLLocationManager()
        locationManager.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
        locationManager.stopUpdatingLocation()
        print(error)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print("Did update location called?")
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
}

回答1:


Problem is here:

  override func viewDidAppear(_ animated: Bool) {
      locationManager = CLLocationManager()
      locationManager.requestWhenInUseAuthorization()
  }

This method will be called after viewDidLoad, but you new created a locationManager later in viewDidAppear and not pointed its delegate to self. That's why the delegate(self)'s methods is not be called.

The improved but not the best way is:

class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        //Ask user for location
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        //Use users current location if no starting point set
        if CLLocationManager.locationServicesEnabled() {
            if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse
              || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways {
                locationManager.startUpdatingLocation()
            }
            else{
                locationManager.requestWhenInUseAuthorization()
            }
        }
        else{
            //Alert user to open location service, bra bra bra here...
        }
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //... nothing need to do for location here
    }

    public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == CLAuthorizationStatus.authorizedWhenInUse
          || status == CLAuthorizationStatus.authorizedAlways {
            locationManager.startUpdatingLocation()
        }
        else{
            //other procedures when location service is not permitted.
        }
    }

    //......
}


来源:https://stackoverflow.com/questions/43484956/location-manager-didupdatelocations-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!