iOS9 后台定期定位
Periodic location in background in iOS9
当应用程序处于后台时,我尝试每隔 "time interval" 在后台使用 locationManager
跟踪位置。
它在 iOS 8 iPhone 6 上运行良好 - 但我在 2.5 - 3 分钟后一直被终止,由 iOS 在 iOS 9 iPhone 6s.
我的主要问题是 - iPhone 6 iOS 8 和 6s iOS 9 之间有什么区别?更好的后台任务看门狗定时器?如果可以,我该如何解决?
我提出的解决方案包括降低准确性以节省电池寿命而不是时间间隔,我不希望这样,因为用户一直在看到位置监控指示(实箭头)。
我的一些代码 -
-(void)initialize
{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = 5;
self.locationManager.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(addMonitorsBeforeWillTerminate)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];
}];
[self.locationManager startUpdatingLocation];
}
谢谢
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
更多的深度和广度信息可以在下面找到。
我推荐两个选项。
- 在后台切换到 kCLLocationAccuracyThreeKilometers。这将关闭 GPS 并且不会耗尽电池,因为它只会使用电池信息。请求后台定位服务的权限并创建一个 NSTimer 回调(这将使您的应用程序 运行 保留在 BG 中)。在回调中切换到所需精度,更新位置,然后再次切换回低精度。
- 报名参加重大地点变更。但是,您的应用不会每隔 X 分钟收到一次通知。
无论将来如何努力,答案来自 "technerd" 的评论,所以我把它变成了答案 -
当应用程序处于后台时,我尝试每隔 "time interval" 在后台使用 locationManager
跟踪位置。
它在 iOS 8 iPhone 6 上运行良好 - 但我在 2.5 - 3 分钟后一直被终止,由 iOS 在 iOS 9 iPhone 6s.
我的主要问题是 - iPhone 6 iOS 8 和 6s iOS 9 之间有什么区别?更好的后台任务看门狗定时器?如果可以,我该如何解决?
我提出的解决方案包括降低准确性以节省电池寿命而不是时间间隔,我不希望这样,因为用户一直在看到位置监控指示(实箭头)。
我的一些代码 -
-(void)initialize
{
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
self.locationManager.distanceFilter = 5;
self.locationManager.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(addMonitorsBeforeWillTerminate)
name:UIApplicationWillTerminateNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];
}];
[self.locationManager startUpdatingLocation];
}
谢谢
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")){
[self.locationManager setAllowsBackgroundLocationUpdates:YES];
}
更多的深度和广度信息可以在下面找到
我推荐两个选项。
- 在后台切换到 kCLLocationAccuracyThreeKilometers。这将关闭 GPS 并且不会耗尽电池,因为它只会使用电池信息。请求后台定位服务的权限并创建一个 NSTimer 回调(这将使您的应用程序 运行 保留在 BG 中)。在回调中切换到所需精度,更新位置,然后再次切换回低精度。
- 报名参加重大地点变更。但是,您的应用不会每隔 X 分钟收到一次通知。
无论将来如何努力,答案来自 "technerd" 的评论,所以我把它变成了答案 -