jjzjj

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

coder 2023-09-06 原文

我为 mapView suing swift 写了一个简单的例子,但我得到打印 Trying to start MapKit location updates without prompting for location authorization.必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。

我将 mapView 添加到 viewController 并开始定位。我还在 startUpdatingLocation()

之前调用了 requestWhenInUseAuthorization()

我设置了Info.plist

现在我同时设置了 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription ,它不起作用。

这是我的代码,有什么问题吗?

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/

有关ios - swift ios9 : Trying to start MapKit location updates without prompting for location authorization的更多相关文章

随机推荐