我为mapView起诉swift编写了一个简单的示例,但得到了Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.打印信息

我将mapView添加到viewController并开始位置。我也叫requestWhenInUseAuthorization()startUpdatingLocation()之前

我设置了Info.plist

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

现在我同时设置了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription,它不起作用。
ios -  swift ios9 : Trying to start MapKit location updates without prompting for location authorization-LMLPHP

Tere是我的代码,怎么了?

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)

    }
}

最佳答案

警告提示您在plist中缺少两个必需的字符串之一

 NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription

关于ios - swift ios9 : Trying to start MapKit location updates without prompting for location authorization,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34013045/

10-09 00:07