我正在寻找一种方法来获得背景位置更新每n分钟在我的iOS应用程序。我使用的是iOS 4.3,解决方案应该适用于未越狱的iphone。

我尝试/考虑了以下选项:

CLLocationManager startUpdatingLocation/startMonitoringSignificantLocationChanges: This works in the background as expected, based on the configured properties, but it seems not possible to force it to update the location every n minutes NSTimer: Does work when the app is running in the foreground but doesn't seem to be designed for background tasks Local notifications: Local notifications can be scheduled every n minutes, but it's not possible to execute some code to get the current location (without the user having to launch the app via the notification). This approach also doesn't seem to be a clean approach as this is not what notifications should be used for. UIApplication:beginBackgroundTaskWithExpirationHandler: As far as I understand, this should be used to finish some work in the background (also limited in time) when an app is moved to the background rather than implementing "long-running" background processes.

如何实现这些定期的后台位置更新?


当前回答

不幸的是,你所有的假设似乎都是正确的,我认为没有办法做到这一点。为了节省电池寿命,iPhone的定位服务是基于移动的。如果手机放在一个位置,定位服务就看不见它。

CLLocationManager只会在手机接收到位置更新时调用locationManager:didUpdateToLocation:fromLocation:,这只会发生在三个位置服务(蜂窝塔,gps, wifi)之一感知到变化时。

其他一些事情可能有助于进一步的解决方案:

Starting & Stopping the services causes the didUpdateToLocation delegate method to be called, but the newLocation might have an old timestamp. Region Monitoring might help When running in the background, be aware that it may be difficult to get "full" LocationServices support approved by Apple. From what I've seen, they've specifically designed startMonitoringSignificantLocationChanges as a low power alternative for apps that need background location support, and strongly encourage developers to use this unless the app absolutely needs it.

好运!

更新:这些想法现在可能已经过时了。看起来人们在@wjans的回答上取得了成功。

其他回答

我使用了xs2bush获取间隔的方法(使用timeIntervalSinceDate),并对其进行了一点扩展。我想确保我得到了我所需要的精度,同时我也没有因为保持gps收音机开得太久而耗尽电池。

我保持位置运行持续与以下设置:

locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locationManager.distanceFilter = 5;

这是一个相对低的电池消耗。当我准备好获得下一个周期性位置读数时,我首先检查位置是否在我期望的精度范围内,如果是,然后使用该位置。如果不是,那么我用这个来提高准确性:

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 0;

get my location and then once I have the location I turn the accuracy back down again to minimize the drain on the battery. I have written a full working sample of this and also I have written the source for the server side code to collect the location data, store it to a database and allow users to view gps data in real time or retrieve and view previously stored routes. I have clients for iOS, android, windows phone and java me. All clients are natively written and they all work properly in the background. The project is MIT licensed.

iOS项目的目标是iOS 6,使用iOS 7的基础SDK。你可以在这里得到代码。

如果你看到任何问题,请在github上提交一个问题。谢谢。

不幸的是,你所有的假设似乎都是正确的,我认为没有办法做到这一点。为了节省电池寿命,iPhone的定位服务是基于移动的。如果手机放在一个位置,定位服务就看不见它。

CLLocationManager只会在手机接收到位置更新时调用locationManager:didUpdateToLocation:fromLocation:,这只会发生在三个位置服务(蜂窝塔,gps, wifi)之一感知到变化时。

其他一些事情可能有助于进一步的解决方案:

Starting & Stopping the services causes the didUpdateToLocation delegate method to be called, but the newLocation might have an old timestamp. Region Monitoring might help When running in the background, be aware that it may be difficult to get "full" LocationServices support approved by Apple. From what I've seen, they've specifically designed startMonitoringSignificantLocationChanges as a low power alternative for apps that need background location support, and strongly encourage developers to use this unless the app absolutely needs it.

好运!

更新:这些想法现在可能已经过时了。看起来人们在@wjans的回答上取得了成功。

在苹果开发者论坛的帮助下,我找到了一个解决方案:

指定位置背景模式 在后台用UIApplication:beginBackgroundTaskWithExpirationHandler创建一个NSTimer: 当n小于UIApplication: backgroundtimerremain时,它会正常工作。当n较大时,应该在没有剩余时间之前再次启用(并禁用)位置管理器,以避免后台任务被终止。

这是因为位置是三种允许的后台执行类型之一。

注意:我在模拟器中测试了一些时间,在那里它不起作用。不过,它在我的手机上运行得很好。

有一个cocoapod APScheduledLocationManager,允许每n秒获得所需位置精度的后台位置更新。

let manager = APScheduledLocationManager(delegate: self)
manager.startUpdatingLocation(interval: 170, acceptableLocationAccuracy: 100)

存储库还包含一个用Swift 3编写的示例应用程序。

似乎stopUpdatingLocation是什么触发后台看门狗定时器,所以我在didUpdateLocation替换它:

     [self.locationManager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
     [self.locationManager setDistanceFilter:99999];

这似乎有效地关闭了GPS。然后,背景NSTimer的选择器变成:

- (void) changeAccuracy {
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
}

我所做的是每隔几分钟定期切换精度以获得高精度坐标,因为locationManager没有停止,backgroundtimerremain保持在其最大值。这将我的设备上的电池消耗从每小时~10%(在后台使用恒定的kCLLocationAccuracyBest)降低到每小时~2%