4

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)

    }   
}
rose
  • 241
  • 5
  • 16

2 Answers2

6

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

 NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription 
Lamour
  • 3,002
  • 2
  • 16
  • 28
0

This might be the problem:

let locationManager = CLLocationManager()

you put let and you already declared like a variable:

var locationManager: CLLocationManager?

use it without "let"

tbodt
  • 16,609
  • 6
  • 58
  • 83
migel
  • 1