CLLocationManager在后台停止 [英] CLLocationManager stops in background

查看:81
本文介绍了CLLocationManager在后台停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用 CLLocationManager 来跟踪用户的路线,并沿着所走的路径绘制点。该应用程序在后台运行,方法是使用必需的后台模式>应用程序注册以进行位置更新。

I have an app which uses CLLocationManager to track the user's route, drawing dots along the path taken. The app runs in the background using Required background modes > App registers for location updates.

据我了解,在后台发生的任何事情都需要从<$ c调用$ c> locationManager:didUpdateToLocation:fromLocation ,因为这是每次位置更新都会调用的方法。

As I understand, anything that happens in the background needs to be called from locationManager:didUpdateToLocation:fromLocation as this is the method that gets called with each location update.

我遇到的问题是有时这会停止被调用。当用户的位置在15分钟左右的时间内变化不大时,似乎会发生这种情况。据我所知,对 locationManager:didUpdateToLocation:fromLocation 的调用只是停止了,大概是为了节省电池。不幸的是,当您返回移动状态时,它不会再次恢复。

The problem I'm having is that sometimes this stops getting called. It seems to happen when the user's location does not change much within the space of maybe 15 minutes or so. As far as I can tell, calls to locationManager:didUpdateToLocation:fromLocation just stop, presumably to save the battery. Unfortunately, it doesn't resume again when you're back on the move.

我认为无法替代此行为,因此我想使用Notification Center通知用户该应用不再记录路线。问题是,应用程序如何知道这已发生?如果未调用 locationManager:didUpdateToLocation:fromLocation ,则无法触发通知。如果被调用,则不会触发该通知。

I presume there's no way to override this behaviour, so I would like to use Notification Centre to inform the user that the app is no longer recording the route. The problem is, how can the app know that this has happened? If locationManager:didUpdateToLocation:fromLocation is not called, I can't fire my notification. If it is being called, the notification should not fire.

推荐答案

我认为没有任何方法可以通知位置管理器已停止向您发送事件,但是有一种方法可以防止它发生。在iOS 6中,添加了一项新功能,如果位置管理器认为服务未被使用,则可以关闭它们。如果您执行以下操作,则位置管理器将继续在后台向您发送事件,直到用完电池为止:

I don't think that there is any way to be notified that the location manager has stopped sending you events, but there is a way to prevent it from happening. In iOS 6, a new feature was added that allows the location manager to power down services if it doesn't think they are being used. If you do the following, the location manager will continue sending you events in the background until you run out of battery:


if([self .locationManager responsesToSelector:@selector(pausesLocationUpdatesAutomatically)])
{
self.locationManager.pausesLocationUpdatesAutomatically = NO;
}

if ([self.locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) { self.locationManager.pausesLocationUpdatesAutomatically = NO; }

此外,在iOS6中,您应该使用 locationManager:didUpdateLocations:已弃用 locationManager:didUpdateToLocation:fromLocation:

Also, in iOS6, you should be using locationManager:didUpdateLocations: as locationManager:didUpdateToLocation:fromLocation: is deprecated.

这篇关于CLLocationManager在后台停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆