在iOS 8中,用户位置警报不会保留在屏幕上

时间:2014-11-24 11:12:32

标签: ios locationmanager

我想在获取他的位置之前获得用户的授权。我正在使用此代码:

        self.locationManager  = [[CLLocationManager alloc] init];
        [self.locationManager setDelegate:self];
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            // iOS8+
            // Sending a message to avoid compile time error
            [self.locationManager requestAlwaysAuthorization];
        }
        [self showIndicatorView];
        self.getCityService = [[GetCitiesService alloc] initServiceWithDelegate:self isLocalCall:NO];
        [self.getCityService fetchCities];

我在屏幕上看到警报,但在我允许之前,它会从屏幕上消失,并且应用程序未获得授权。

我希望我的代码停止,直到用户授予权限。

2 个答案:

答案 0 :(得分:2)

显然在iOS 8 SDK中,在开始位置更新之前,需要requestAlwaysAuthorization(用于后台位置)或requestWhenInUseAuthorization(仅在前台时的位置)调用CLLocationManager。

Info.plist中还需要有NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription键,并在提示中显示消息。添加这些解决了我的问题。

希望它可以帮助别人。

编辑:有关更多信息,请查看:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

答案 1 :(得分:1)

你的代码看起来很奇怪,因为你似乎在调用requestAlwaysAuthorization两次。一旦通过self.locationManager,一次通过sendAction

您的代码应如下所示:

    self.locationManager  = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        // iOS8+
        // Sending a message to avoid compile time error
        [self.locationManager requestAlwaysAuthorization];
    }
相关问题