CoreLocation在ios 8中不起作用

时间:2014-10-30 12:52:08

标签: ios core-location

我在我的应用中使用了位置服务。

但在[CLLocationManager authorizationStatus]

之前kCLAuthorizationStatusNotDetermined [self.locationManager startUpdatingLocation];#import <CoreLocation/CoreLocation.h> @interface NewRunViewController () <CLLocationManagerDelegate> @property (nonatomic, strong) CLLocationManager *locationManager; - (void)viewDidLoad { [super viewDidLoad]; [self startLocationUpdates]; } - (void)startLocationUpdates { // Create the location manager if this object does not // already have one. if (self.locationManager == nil) { self.locationManager = [[CLLocationManager alloc] init]; } self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.distanceFilter = 10; if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } [self.locationManager startUpdatingLocation]; }

项目代码:

实施

NSLocationWhenInUseUsageDescription

我还将info.plist密钥添加到{{1}}文件中。 并且应用程序也不会要求用户使用核心位置服务的权限。 任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

您必须为目标包含密钥NSLocationAlwaysUsageDescription的字符串值。

在Xcode中查看目标的“信息”页面。当系统要求用户允许您在应用中使用位置服务时,该字符串将成为警报的文本。

这是我自己处理相同的代码。它正在请求始终授权,但您可以替换为使用时。

  CLAuthorizationStatus auth = [CLLocationManager authorizationStatus];
  if (auth == kCLAuthorizationStatusNotDetermined && [self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
      [self.locationManager performSelector:@selector(requestAlwaysAuthorization) withObject:NULL];
  } else {
      if (auth == kCLAuthorizationStatusAuthorizedAlways) {
          [self notificationsSetup];
      } else {
          NSLog(@"Device is not authorized for proper use of the location Services, no logging will be performed");
      }
  }

您可能会发现我的github存储库很有用 - TTLocationHandler on Githup

答案 1 :(得分:1)

你必须搬家

[self.locationManager startUpdatingLocation];

到委托方法

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        {
            NSLog(@"User still thinking");
        }
        break;
        case kCLAuthorizationStatusDenied:
        {
            NSLog(@"User denied location request");
        }
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        case kCLAuthorizationStatusAuthorizedAlways:
        {
            [self.locationManager startUpdatingLocation];
        }

            break;
        default:
            break;
    }
}

更新:让日志更易于理解

答案 2 :(得分:0)

谢谢你们的回答。我找到了问题的解决方案。我检查了系统的版本:

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

在startLocationUpdates方法中:

if(IS_OS_8_OR_LATER) {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }
}

现在它正常运作。