为什么iOS8中的CLLocationManager startUpdatingLocation没有?

Adr*_*man 3 ios8

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

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

这在iOS 7.0.3模拟器中工作正常.

我哪里错了?

Adr*_*man 9

iOS 8要求您在调用之前调用CLLocationManager.requestWhenInUseAuthorizationCLLocationManager.requestAlwaysAuthorizationCLLocationManager.startUpdatingLocation.

@interface MyViewController : UIViewController <CLLocationManagerDelegate>

@property CLLocationManager *locationManager;

@end
Run Code Online (Sandbox Code Playgroud)

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 {
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

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