为什么iOS8中的CLLocationManager startUpdatingLocation没有?

时间:2014-09-19 11:52:10

标签: ios8

我使用XCode 6附带的模拟器测试iOS 8.0中的现有位置功能。我无法CLLocationManager.startUpdatingLocation触发CLLocationManagerDelegate.locationManager:didUpdateLocationsCLLocationManagerDelegate.locationManager:didFailWithError

我没有得到""我的应用程序"想要使用您当前的位置"警告,就像我在iOS 7.0.3模拟器上做的那样。我已使用Google地图应用验证模拟的位置设置。

这在iOS 7.0.3模拟器中运行良好。

我在哪里错了?

5 个答案:

答案 0 :(得分:9)

iOS 8要求您在致电CLLocationManager.startUpdatingLocation之前致电CLLocationManager.requestWhenInUseAuthorizationCLLocationManager.requestAlwaysAuthorization

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

@property CLLocationManager *locationManager;

@end

requestWhenInUseAuthorizationrequestAlwaysAuthorization异步运行,因此您需要确保在使用可以响应警报之前清除CLLocationManager对象。

- (void) viewDidAppear:(BOOL)animated {
    if ([CLLocationManager locationServicesEnabled]) {
        self.locationManager = [CLLocationManager new];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

        SEL selector = NSSelectorFromString(@"requestWhenInUseAuthorization");
        if ([self.locationManager respondsToSelector:selector]) {
            [self.locationManager requestWhenInUseAuthorization];
        } else {
            [self.locationManager startUpdatingLocation];
        }
    } else {
        ...
    }
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self.locationManager startUpdatingLocation];
    } else if (status == kCLAuthorizationStatusAuthorized) {
        // iOS 7 will redundantly call this line.
        [self.locationManager startUpdatingLocation];
    } else if (status > kCLAuthorizationStatusNotDetermined) {
        ...
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    ...
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    ...
}

当您实施CLLocationManagerDelegate时,您现在也需要实施locationManager:didChangeAuthorizationStatus:方法。在此方法中,您可以检查用户是否已授予应用程序权限,并采取相应措施。

如果 [CLLocationManager authorizationStatus] = nil ,则当authorizationStatus设置为 locationManager:didChangeAuthorizationStatus:时,将调用 kCLAuthorizationStatusNotDetermined 当用户从警告对话框中进行选择时,再次

答案 1 :(得分:2)

...还要加入Adrian Toman的回答

不要忘记将NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription添加到 application-Info.plist 文件中。

将属性添加到plist后,还需要为其添加字符串值,当您要求他们使用其位置时,该字符串值将显示给用户。

答案 2 :(得分:2)

-(void)statLocationManager
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    [locationManager requestWhenInUseAuthorization]; // Add this Line


}

在Info.plist中添加以下内容

enter image description here

答案 3 :(得分:0)

除了Adrian Toman的答案,如果设备是WiFi,只能尝试禁用/启用位置设置。

答案 4 :(得分:0)

我曾经遇到过这个问题,当我拨打startUpdatingLocation时,locationManager:(CLLocationManager *)manager didUpdateLocations:没有得到回应。即使我遵循了其他人提供的所有指令,我也无法触发此方法。

最后我意识到有一件事是关键的,它需要将locationManager的委托设置为&#34; self&#34; (您实现方法locationManager:(CLLocationManager *)manager didUpdateLocations:)的地方。

最终,一切顺利。