I write a easy example for mapView suing swift, but I get the print Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
I add a mapView to viewController and start location. I also call requestWhenInUseAuthorization()
before startUpdatingLocation()
I set the Info.plist
now I set both NSLocationAlwaysUsageDescription
and NSLocationWhenInUseUsageDescription
, it doesn't work.
Tere is my code, what's wrong?
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
var mapView: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 10
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = true
if CLLocationManager.locationServicesEnabled() {
let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined {
if #available(iOS 8.0, *) {
locationManager.requestWhenInUseAuthorization()
}
}
locationManager.startUpdatingLocation()
self.locationManager = locationManager
} else {
print("locationServices disenabled")
}
let mapview = MKMapView(frame: self.view.bounds)
mapview.mapType = .Standard
mapview.showsUserLocation = true
mapview.delegate = self
self.mapView = mapview
self.view.addSubview(mapview)
}
}