我需要创建一个在后台模式下检索 iOS radio 信息(rssi、承载等)的应用程序;该应用程序将在 App Store 上可用,因此符合 App Store 指南。 应用应合理保持手机电量。
我目前同时使用 BackgroundFetch 和 GPS,效果很好,但是当 iOS 没有足够的内存时,应用程序会被操作系统杀死。
我还查看了使用 GPS 并保持 iPhone 电池生命周期的 Google 应用程序 (Google Now),但我不知道他们是怎么做到的。
谢谢
最佳答案
- 确保您的 Plist 中启用了“后台获取”和“位置更新”。
- 当您的应用在后台时,用户使用 startMonitoringSignificantLocationChanges 而不是 startUpdatingLocation。以下是文档中的一些信息。
[startUpdatingLocation] 通常需要启用位置跟踪硬件更长的时间,这会导致更高的功耗。” “[使用 startMonitoringSignificantLocationChanges] 一旦设备从之前的通知移动 500 米或更远,应用就会收到通知。通知的频率不应超过每五分钟一次。”
我会浏览此处的文档,以便您可以更好地理解 ( https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html#//apple_ref/occ/instm/CLLocationManager/startMonitoringSignificantLocationChanges )
正如您可能看到的,使用 startMonitoringSignificantLocationChanges 可以显着延长电池生命周期。以下是我在 AppDelegate.m 中实现它的方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[self.locationManager startMonitoringSignificantLocationChanges];
...
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
...
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
...
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
...
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
...
}
-(CLLocationManager *)locationManager{
if(!_locationManager){
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.distanceFilter = 5;
}
return _locationManager;
}
通过这种方式,我确保仅在应用处于事件状态时使用耗电的“startUpdatingLocation”方法,而在非事件时使用省电的“stopMonitoringSignificantLocationChanges”方法。
关于iOS 后台应用程序 : best way to run app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22297969/