【问题标题】:CLLocation manager does not update locations if startUpdatingLocation is called when App is in background [duplicate]如果 App 在后台时调用 startUpdatingLocation,CLLocation 管理器不会更新位置 [重复]
【发布时间】:2016-04-13 10:02:59
【问题描述】:

我在我的应用程序中使用CLLocationManager 并将pausesLocationUpdatesAutomatically 属性设置为YES 以节省电池。当位置暂停时,然后我尝试使用startUpdatingLocation 方法在指定的时间间隔后重新启动位置更新,它启动并给出7 到 10 次位置更新。之后我没有得到任何进一步的位置更新。我调用下面的方法重新开始更新。

PS:我已经在功能中启用了位置背景模式。

[_locManager startUpdatingLocation];
                if ([_locManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)])
                {
                    _locManager.allowsBackgroundLocationUpdates =YES;
                } 

【问题讨论】:

  • @kb920 请再次阅读我的问题。我已经将此属性设置为 YES。
  • 你使用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{ }delegate 方法对吗?
  • @Vvk yes didUpdateLocations 正在使用中。
  • 一一尝试两种方法-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { } -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { }
  • 我知道的唯一解决方法(我从副本中找到)是在前台调用 startMonitoringSignificantLocationChanges。如果您这样做,那么您似乎可以在后台调用 `startUpdatingLocations 并无限期地获取更新

标签: ios objective-c xcode cllocationmanager background-process


【解决方案1】:

转到项目构建设置,在目标选择下选择功能 - 打开后台模式 - 选中框位置更新。

我认为这可以解决你的问题。

【讨论】:

  • 该属性已经存在。一旦位置更新由于 pausesLocationUpdatesAutomatically 属性而被操作系统暂停,我的代码将无法工作。
  • 当应用程序进入前台时,它会更新吗?
  • 是的,然后它会更新。
  • 我认为如果您将 pausesLocationUpdatesAutomatically 设置为 yes,那么由于省电方案,它不允许后台执行。
  • 但它在开始时会给出 7 到 10 个位置,但之后它不会给出位置
猜你喜欢
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-30
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多