Ios MapView没有';不要请求位置许可

Ios MapView没有';不要请求位置许可,ios,swift,mkmapview,Ios,Swift,Mkmapview,我正在尝试用Swift构建一个地图视图。它已经起作用了,但是在我改变了一些我不记得的东西之后,我现在得到了以下错误: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthoriz

我正在尝试用Swift构建一个地图视图。它已经起作用了,但是在我改变了一些我不记得的东西之后,我现在得到了以下错误:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
NSLocationAlwaysUsageDescription
位于my.plist文件中

代码如下:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
    var map: MKMapView?
    var manager: CLLocationManager?

    func setup() {

        manager = CLLocationManager()
        manager!.delegate = self

        map!.delegate = self // map is being set from another controller

        manager!.requestAlwaysAuthorization()
        manager!.startUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        println("permisison did change")
        if(status == CLAuthorizationStatus.AuthorizedWhenInUse || status == CLAuthorizationStatus.Authorized) {
          map!.showsUserLocation = true
        }
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        for location in (locations as Array) {
            var loc = (location as CLLocation)
            println(loc.coordinate.latitude)

            let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
            map!.setRegion(region, animated: true)
        }
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("fail")
    }

}
在实现一些建议之前使用旧代码:

import UIKit
import MapKit

class AwesomeMap : UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var manager = CLLocationManager()
var map: MKMapView?

func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    // the change I made was around the following six lines I think...

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization()

    map!.showsUserLocation = true
    manager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    for location in (locations as Array) {
        var loc = (location as CLLocation)
        println(loc.coordinate.latitude)

        let region = MKCoordinateRegion(center: loc.coordinate, span: MKCoordinateSpanMake(0.05, 0.05))
        map!.setRegion(region, animated: true)
    }
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("fail") // nothing happens here
}

}

我的应用程序中有此工作代码(减少):

此外,您还可以对任何用户的授权状态更改做出反应:

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        manager.startUpdatingLocation()
    }
}

你只能打电话

map!.showsUserLocation = true
在您的位置管理器获得使用用户位置的权限后

比如在(没有这方面的快速版本,但你可能会明白这一点)


授权状态未确定时,请勿调用showsUserLocation

func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization() // Does not if status is determined.

    self.locationManager(manager: manager, status: CLLocationManager.authorizationStatus())
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        map!.showsUserLocation = true
        manager.startUpdatingLocation()
    }
}

转到模拟器设置

之后,进入定位服务并打开。如果需要MapView中的位置蓝色点,请输入并设置>


>设置>“>

谢谢你的回答,但我在使用你的代码时仍然会遇到同样的错误。啊,是的,我明白了,谢谢。我用Swift实现了它,但实际上什么也没有发生。我将用当前代码更新我的问题。@Nikolaj尝试将var manager=CLLocationManager()声明为类变量,如”var-map:MKMapView?”,它可能在代理响应之前被解除分配。这是个好主意,但据我所知没有起作用。无论如何,谢谢!
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
     if(status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways)
    {
        self.mapView.showsUserLocation = YES;
    }
}
func setup(mapView: MKMapView) { // passing the IBOutlet from another controller

    map = mapView
    map!.delegate = self

    manager.delegate = self
    manager.requestAlwaysAuthorization() // Does not if status is determined.

    self.locationManager(manager: manager, status: CLLocationManager.authorizationStatus())
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .Authorized {
        map!.showsUserLocation = true
        manager.startUpdatingLocation()
    }
}