【问题标题】:didUpdateLocations not called未调用 didUpdateLocations
【发布时间】:2013-11-06 15:07:55
【问题描述】:

我正在尝试获取我的当前位置,但 didUpdateLocations 中的断点从未被调用。

位置管理器:

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager setDesiredAccuracy:kCLDistanceFilterNone];
[locationManager startUpdatingLocation];

委托方法:

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

我确认定位服务并启用和授权。

为什么没有像应有的那样调用 locationManager 委托方法?

【问题讨论】:

  • 几分钟前遇到了同样的问题,我还没有完成[[CLLocationManager alloc] init];。这是帮助我的link
  • 尝试删除此行:[locationManager setDesiredAccuracy:kCLDistanceFilterNone];,因为您设置了两次 setDesiredAccuracy,并使 locationManager 成为属性

标签: ios objective-c cllocationmanager cllocation


【解决方案1】:

此外,在 iOS8 中你必须有两个额外的东西:

  • 将密钥添加到您的Info.plist 并请求位置管理员授权以启动它。

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • 您需要为相应的定位方式申请授权。

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

代码示例:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];

来源:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

【讨论】:

  • 对我来说是完美的答案。
  • 对我不起作用。有一些东西不见了。 This answer 工作。
  • 请注意,在 iOS 11 中,必须向 info.plist 提供第三个密钥:NSLocationAlwaysAndWhenInUseUsageDescription
【解决方案2】:

当我遇到这个问题时,是由于线程问题。

确保所有这些方法都在主线程上调用。 非常重要的是,不仅startUpdatingLocation方法在主线程上被调用,其他的也是如此。

你可以强制代码在主线程上运行,方法是把它包裹在里面

dispatch_sync(dispatch_get_main_queue(), ^{

});

还可以查看this answer

【讨论】:

  • 我遇到了与上面相同的问题,但是当我使用您的解决方案时,之后我的按钮无法点击。很奇怪。
  • @Tim Bodeit 谢谢老兄...保存我的答案。
  • 一定忽略了……你说的是什么按钮?
  • 根据我的经验,dispatch_async() 就足够了……它可能会解决界面更新问题——只是猜测@dulax
【解决方案3】:

确保您已将 CLLocationManager 添加为属性。

@property (nonatomic , strong) CLLocationManager *locationManager;

【讨论】:

  • 看起来这对我有帮助 - 否则我不断收到 EXC_BAD_ACCESS 错误。谢谢:)
【解决方案4】:

是的,该物业是我的解决方案,检查定位服务是否启用是个好主意:

if ([CLLocationManager locationServicesEnabled]) {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
}

【讨论】:

    【解决方案5】:

    您必须告诉模拟器要模拟的位置。如果您不指定位置,您的CLLocationManager 委托方法将永远不会被调用。您可以使用模拟器菜单 Debug -> Location。同样在调试区域下方的 Xcode 中,从 Xcode 运行应用程序时会出现一个小位置箭头。您可以使用它来指定 GPX 文件来模拟运动(但它仍然与真实设备不同)。

    https://devforums.apple.com/message/1073267#1073267

    【讨论】:

      【解决方案6】:

      如果设置了 CLLocationManagerDelegate,则也设置了 MapView Delegate

      还要检查模拟器的位置,点击模拟器>调试>位置, 如果没有更改为城市运行或高速公路驱动器。它对我有用。

      【讨论】:

        【解决方案7】:

        请注意,在 iOS 11 及更高版本中,必须向 info.plist 提供第三个密钥:NSLocationAlwaysAndWhenInUseUsageDescription

        【讨论】:

          【解决方案8】:

          在我将CLLocationManagerdesiredAccuracy 属性更改为kCLLocationAccuracyKilometer 的值后,我的应用开始有时会在第一次调用didUpdateLocations 时出现数分钟延迟:

          myLocationManager.desiredAccuracy = kCLLocationAccuracyKilometer; 
          [myLocationManager startUpdatingLocation]; // Occasional long delay
          

          将其改回kCLLocationAccuracyBest 后,对didUpdateLocations 的调用再次开始总是立即发生:

          myLocationManager.desiredAccuracy = kCLLocationAccuracyBest; 
          [myLocationManager startUpdatingLocation]; // Calls didUpdateLocations immediately
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-24
            • 2011-07-19
            • 2017-09-15
            • 2014-11-28
            • 1970-01-01
            相关资源
            最近更新 更多