即使我在iOS 8中使用swift请求授权,也会出现位置授权错误

时间:2015-03-11 21:17:04

标签: objective-c swift ios8 cllocationmanager

我有一个使用位置服务的应用。如果应用程序首次启动,则会询问用户是否允许。 出于某种原因,如果我点击“允许”,我会收到此消息:

Trying to start MapKit location updates without prompting for location authorization.

我知道这意味着什么,但我已经在我的代码中设置了断点,我确定在允许这样做之前没有任何东西试图读取用户位置。

无论如何,我似乎错过了一些东西。

1)是否存在人们可以做的“常见错误”,故事板内的某些内容? 2)Apple会拒绝有这样错误的应用程序吗?

事情是应用程序运行得很好,唯一的事情就是我在控制台中看到了这条消息。我不知道Apple是否会看到此消息,如果这是拒绝应用程序的原因..

4 个答案:

答案 0 :(得分:1)

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { // iOS8+
    // Sending a message to avoid compile time error
    [[UIApplication sharedApplication] sendAction:@selector(requestWhenInUseAuthorization)
                                               to:self.locationManager
                                             from:self
                                         forEvent:nil];
} else {
    [self.locationManager startUpdatingLocation];
}

}

我认为您可能需要在requestWhenInUseAuthorization

中包含类似的内容

答案 1 :(得分:0)

假设您正在使用CLLocationManager。您是否对 locationManager 对象进行了强大引用?

当您在函数内部请求本地范围(变量)中的位置时,似乎就是这种情况。然后尝试使用MapKit,但是已经解除分配了locationManager对象。

要解决这种情况,你应该声明......

var locationManager = CLLocationManager()

...作为实例变量,然后请求授权,然后使用位置服务。

答案 2 :(得分:0)

问题是如果应用程序正在使用中,我已经要求授权使用位置服务,并且我已经确定了这样的授权:

func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = GpsStatus.restricted
                break
            case CLAuthorizationStatus.Denied:
                locationStatus = GpsStatus.denied
                break
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = GpsStatus.notDeterminded
                break
            default:
                locationStatus = GpsStatus.allowed
                break
            }
    }

这似乎是错误的,如果我明确检查CLAuthorizationStatus.AuthorizedWhenInUse,错误就消失了:

func locationManager(manager: CLLocationManager!,
        didChangeAuthorizationStatus status: CLAuthorizationStatus) {
            switch status {
            case CLAuthorizationStatus.Restricted:
                locationStatus = GpsStatus.restricted
                break
            case CLAuthorizationStatus.Denied:
                locationStatus = GpsStatus.denied
                break
            case CLAuthorizationStatus.NotDetermined:
                locationStatus = GpsStatus.notDeterminded
                break
            case CLAuthorizationStatus.AuthorizedWhenInUse:
                locationStatus = GpsStatus.allowed
            default:
                locationStatus = GpsStatus.notDeterminded
                break
            }
    }

编辑:

在我获得使用位置服务的权限之前添加TrackingLocationButton似乎也是一个问题。

这样做

     var userTrackingButton = MKUserTrackingBarButtonItem(mapView: self.mapView);
        self.toolBar.items?.insert(userTrackingButton, atIndex: 0)

只有您拥有权限

答案 3 :(得分:-1)

对于iOS 8,您需要定义"隐私 - 位置使用说明"在Info.plist中。

EG。 NSLocationWhenInUseUsageDescription ="使用您的位置在商店附近显示"。

NSLocationAlwaysUsageDescription ="使用您的位置在商店附近显示"。

此键指定访问用户位置信息的原因。