rose rose - 5 months ago 39
iOS Question

swift ios9: Trying to start MapKit location updates without prompting for location authorization

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

enter image description here

now I set both
NSLocationAlwaysUsageDescription
and
NSLocationWhenInUseUsageDescription
, it doesn't work.
enter image description here

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)

}
}

Answer

As the warning is telling you are missing one of the two required Strings into your plist

 NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription 
Comments