尽管添加了NSLocationWhenUseUsageDescription,iOS 8定位服务仍无法工作



我正在使用iOS8 [8.0.2],无论我尝试什么,我都无法使CLLocationManager工作。

我在info.plist中添加了以下行。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app use location services.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app use location services.</string>

在我的实现文件中:

In ViewDidLoad : 
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];

但是以下方法都没有被调用。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

我错过了什么?

查看这篇文章。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

上述委派在iOS 6中已被弃用。使用以下内容:

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

编辑

在通过locationManager:didUpdateLocations: 接收授权之前,不要将mapView.showUserLocation设置为YES

这样设置:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    self.mapView.showUserLocation = YES;
    [locationManager stopUpdatingLocation];
}

我也处于同样的情况,这个答案让我在挣扎了很长时间后找到了解决方案。

您需要使locationManager成为控制器的属性。

我应该注意到,这是我和斯威夫特一起想到的。

最新更新