【问题标题】:CLLocation didupdatetolocation not calledCLLocation didupdatetolocation 未调用
【发布时间】:2014-11-30 20:25:40
【问题描述】:

花了几天时间试图解决这个问题,但找不到任何可行的解决方案。我检查了 stackoverflow 上的所有帖子并尝试了他们所有的解决方案,但似乎没有任何效果。我还尝试了适用于我的 CLLocation 的苹果测试项目。我使用了苹果测试项目中的点点滴滴

https://developer.apple.com/library/ios/samplecode/LocateMe/Listings/README_md.html

但是我的代码根本不起作用。 DidupdateToLocation 永远不会被调用。

这是我的代码(在 viewDidLoad 中)

_locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;



// This is the most important property to set for the manager. It ultimately determines how the manager will

// attempt to acquire location and thus, the amount of power that will be consumed.

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;



// Once configured, the location manager must be "started"

//

// for iOS 8, specific user level permission is required,

// "when-in-use" authorization grants access to the user's location

//

// important: be sure to include NSLocationWhenInUseUsageDescription along with its

// explanation string in your Info.plist or startUpdatingLocation will not work.

//

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

    [self.locationManager requestWhenInUseAuthorization];

}

[self.locationManager startUpdatingLocation];



[self performSelector:@selector(stopUpdatingLocationWithMessage:)

           withObject:@"Timed Out"

           afterDelay:30];

我已检查以确保 locationServicesEnabled 已启用。

我已将 NSLoationWhenInUseUsageDescription 属性添加到 info.plist 文件中。我需要添加或启用任何服务的其他属性吗??

看在上帝的份上,我无法弄清楚我做错了什么。有人可以帮我解决这个问题吗?

【问题讨论】:

  • btw 授权提示弹出?
  • 授权提示没有弹出
  • 那么我猜 didChangeAuthorizationStatus 回调没有被调用,对吧?应用程序的位置服务是否已启用? (设置/隐私/位置服务)您确定您在 .plist 中添加了正确的密钥吗?
  • 嘿,大卫,是的,我已经检查过我正在向 plist 添加正确的密钥。仍然无法让它工作。不得不把这件事搁置一段时间。今天会尝试修复它

标签: ios objective-c gps location


【解决方案1】:

在 iOS8 上收到 didChangeAuthorizationStatus 回调后必须调用 startUpdatingLocation。

//iOS 8 API change
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
    [self.locationManager requestWhenInUseAuthorization];
}else{
    [self.locationManager startUpdatingLocation];
}

实现这个委托方法:

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied:
        {
            // do some error handling
        }
            break;
        default:{
            [self.locationManager startUpdatingLocation];
        }
            break;
    }
}

【讨论】:

    【解决方案2】:

    首先,不要忘记,使用 IOS 8 你需要做[locationManager requestWhenInUseAuthorization] 加上[locationManager requestAlwaysAuthorization];

    _locationManager = [CLLocationManager new];
    
    if(SYSTEM_IS_OS_8_OR_LATER) {
        [_locationManager requestWhenInUseAuthorization];
        [_locationManager requestAlwaysAuthorization];
    
    }
    
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
     //and the problem with your code : don't forget to start
    _locationManager startUpdatingLocation];
    

    在你的didUpdateLocations

     - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
            _currentLocation = [locations objectAtIndex:0]; 
            //do your stuff with the location
    
        }
    

    【讨论】:

    • 太棒了。添加requestWhenInUseAuthorizationrequestAlwaysAuthorization 是解决方案。
    【解决方案3】:

    我傻了。我将关键字添加到错误的 plist 文件中。我现在已经开始工作了。谢谢你们的帮助。

    【讨论】:

      【解决方案4】:

      我只是花了整个上午在这[老实说我是相对较新的 OOC],反正问题..

      didUpdateLocation 编译并且从不被调用 didUpdateLocations 编译并运行!

      在这种情况下看起来不是答案,但它在问题中,是一个容易犯的错误。使用 IOS 8.1.3 & Xcode 6.1.1

      【讨论】:

        【解决方案5】:

        只需在 Info.plist 中添加三个属性。

        获取当前位置,如果不是调用didUpdateToLocation方法

        NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription privacy - location usage description,字符串类型。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-03
          • 1970-01-01
          • 2014-04-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-29
          相关资源
          最近更新 更多