我正在尝试实施延迟的位置更新,以获得更好的电池消耗。 我正在这样开始我的位置经理:
- (void)initCoreLocation
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.pausesLocationUpdatesAutomatically = YES;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
//Très important pour iOS9 !
if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
self.locationManager.allowsBackgroundLocationUpdates=YES;
}
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
以这种方式启动延迟更新:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
if (!self.deferringUpdates) {
[self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:30];
self.deferringUpdates = YES;
}
}
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error { // Stop deferring updates
if(error) {
NSLog(@"error");
}
NSLog(@"didFinishDeferredUpdates");
self.deferringUpdates = NO;
}
我每隔30秒就会didFinishDeferredUpdates
一次登录,但是didUpdateLocations
每秒都会保持呼叫,不再尝试优化电池消耗。假设位置管理员每隔30秒调用一次didUpdateLocations
吗?
答案 0 :(得分:0)
也许你没有采用正确的方法,left: calc( 50% - half of the div of the svg)
告诉GPS硬件在内部存储新的位置,直到满足指定的距离或超时条件。
来自iOS开发者库:
如果您的应用位于前台,则位置管理员不会推迟事件的传递,但会监控指定的条件。如果您的应用在符合条件之前移至后台,则位置管理员可能会开始推迟发送事件。 Link
答案 1 :(得分:0)
你在调试吗?
如答案中所述:ios deferred location updates fail to defer
仅在系统进入低功耗状态时才会传递延迟更新。调试期间不会发生延迟更新,因为Xcode会阻止您的应用程序休眠,从而阻止系统进入低功耗状态。
答案 2 :(得分:-1)
我相信您一次只需要其中一个,更改applicationDidEnterBackground
和applicationWillEnterForeground
- (**void)applicationDidEnterBackground:(UIApplication *)application {
// Need to stop regular updates first
[self.locationManager stopUpdatingLocation];
// Only monitor significant changes
[self.locationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[self.locationManager stopMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}