I already posted about this but the answer given did not solve my issue, though it does seem like better code than what I had previously, so I'm using it.
For some reason didUpdateLocations
is not being called, even when I set the delegate to the view controller. Neither is the didFailWithError
ever called, so it's not like I am even throwing an error. But I know that didChangeAuthorization
is being called.
Also 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?
Code
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)")
}
}