即使在didChangeAuthorization中调用locationManager.startUpdatingLocation()时,也没有调用位置管理器didUpdateLocations

时间:2017-04-19 03:00:32

标签: ios swift xcode core-location cllocationmanager

我已经发布了相关信息,但给出的答案并没有解决我的问题,虽然它看起来比我之前的代码更好,所以我使用它。

由于某种原因didUpdateLocations未被调用,即使我将委托设置为视图控制器也是如此。 didFailWithError也没有被调用过,所以它不像我甚至抛出一个错误。但我知道didChangeAuthorization正在被召唤。

同样在info.plist我将密钥Privacy - Location When In Use Usage Description设置为说明。

所以我不确定我做错了什么?

代码

import MapKit
import CoreLocation

class OptionsViewController: UIViewController, UITableViewDelegate, CLLocationManagerDelegate {
    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...
    }
    }

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

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){
    locationManager.stopUpdatingLocation()
    print("didFailWithError")
    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)")
}
}

0 个答案:

没有答案
相关问题