我已经发布了相关信息,但给出的答案并没有解决我的问题,虽然它看起来比我之前的代码更好,所以我使用它。
由于某种原因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)")
}
}