【问题标题】:didUpdateLocations not being called when app is in background应用程序处于后台时未调用 didUpdateLocations
【发布时间】:2016-02-24 17:57:43
【问题描述】:

当应用程序处于活动状态并在前台运行时,didUpdateLocations 工作正常,但一旦应用程序进入后台,它就会停止工作

在.h文件中

@property (nonatomic, strong) CLLocationManager *customerLocationManager;

在 .m 文件中

@synthesize customerLocationManager;



customerLocationManager = [[CLLocationManager alloc] init];
customerLocationManager.delegate = self;
customerLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
customerLocationManager.distanceFilter = kCLDistanceFilterNone;

if(IS_OS_8_OR_LATER) {
    [customerLocationManager requestAlwaysAuthorization];
}
[customerLocationManager startUpdatingLocation];

我在 info.plist 中集成了 NSLocationAlwaysUsageDescription。已启用后台模式并已选择位置更新选项

【问题讨论】:

  • 您的 CLLocationManager 是一个局部变量。大错。它应该是某种持久对象,例如一个实例变量/属性。
  • @matt 我已将 CLLocation 管理器声明为属性,但问题仍然存在
  • 您发布的代码使用局部变量来保存位置管理器。如果这不是您的实际代码,请显示您的实际代码。
  • @DuncanC ,请查看编辑后的实际代码
  • 得到了解决方案,我没有使用allowBackgroundLocationUpdates

标签: ios cllocationmanager


【解决方案1】:

我在 info.plist 中集成了 NSLocationAlwaysUsageDescription。已启用后台模式并已选择“位置更新”选项

很好,但在 iOS 9 中还有一个要求:您必须将位置管理器的 allowsBackgroundLocationUpdates 设置为 YES。您没有这样做,因此您不会获得后台位置更新。

【讨论】:

  • @matt,这工作大约 10 分钟并停止更新,知道为什么吗?如果我将应用程序带到前台,它会再次启动。
  • @Prasad "这工作大约 10 分钟,然后停止更新,知道为什么吗?"那么你没有正确配置应用程序在后台运行。
  • @matt,是的,我相信我做到了。我在 plist 中添加了 NSLocationAlwaysUsageDescription 和 UIBackgroundModes,并初始化了 LocationManager 并将 allowBackgroundLocationUpdates 设置为 YES 然后开始位置更新。上面的代码和我的一样。
  • 你的应用是否获得了后台定位授权?
  • 是的。我调用 requestAlwaysAuthorization 方法来请求它,并在出现提示时允许它。它在“设置”应用的“位置服务”下列为“始终”。
猜你喜欢
  • 2013-10-14
  • 2020-05-09
  • 2017-01-26
  • 2017-11-06
  • 1970-01-01
  • 2022-09-27
  • 2018-02-07
  • 1970-01-01
  • 2021-10-02
相关资源
最近更新 更多