【问题标题】:CLLocation won't start updating locationCLLocation 不会开始更新位置
【发布时间】:2014-10-09 14:04:19
【问题描述】:

更新到 iOS 8 后我遇到了很多麻烦。以下代码在我更新到 iOS 和 Xcode 6 之前有效。代码是在 viewDidLoad 中编写的:

self.locationManager.delegate = self;
[self.locationManager requestWhenInUseAuthorization];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];

在我的 .h 文件中,我将 locationManager 作为属性,同时声明 CLLocationManagerDelegate:

@property CLLocationManager *locationManager;

我在里面设置了断点

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

但代码从未运行过。

我错过了什么步骤???

编辑:请注意,要求用户允许定位服务的提示从未出现。我怀疑这是问题所在,但在我将委托声明为 self 之后,我确实请求了服务。

也添加了

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your message goes here</string>

但仍然不适合我

【问题讨论】:

标签: ios ios8 xcode6 cllocationmanager cllocation


【解决方案1】:

如果您在用户同意使用位置服务器之前开始定位

ios8或更高版本改为

- (xx)xxxx
{
   self.locationManager.delegate = self;
   self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;   
   if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
       [self.locationManager startUpdatingLocation];
   }else{
      [self.locationManager requestWhenInUseAuthorization];
   }     
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{ 
   if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
      [self.locationManager startUpdatingLocation];
   }
}

【讨论】:

    【解决方案2】:

    您需要将NSLocationWhenInUseUsageDescription 密钥添加到您的 info.plist。 (根据我的测试,它可以是空字符串,但密钥必须存在。但不确定 Apple 是否未通过空字符串审核。)

    【讨论】:

    • 我已经在NSLocationWhenInUseUsageDescription中添加了你的消息在这里,但是还是不行。
    【解决方案3】:

    在开始位置更新之前,您需要在 CLLocationManager 上使用 requestAlwaysAuthorization(用于后台位置)或 requestWhenInUseAuthorization(仅在前台时定位)调用。

    Info.plist 中还需要有 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键,并在提示中显示一条消息。

    更多信息在这篇文章中:Location Services not working in iOS 8

    【讨论】:

      【解决方案4】:

      所以在所有“iOS 8”问题之后,我意识到我的问题是我没有分配和初始化我的 locationManager

      self.locationManager = [[CLLocationManager alloc] init];
      self.locationManager.delegate = self;
      [self.locationManager requestWhenInUseAuthorization];
      self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      
          if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) {
              [self.locationManager startUpdatingLocation];
          }else{
              [self.locationManager requestWhenInUseAuthorization];
              [self.locationManager requestAlwaysAuthorization];
          }
      [self.locationManager startUpdatingLocation];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 2011-04-23
        • 1970-01-01
        • 1970-01-01
        • 2017-08-15
        相关资源
        最近更新 更多