获取用户位置每n分钟应用被切换到后台后, [英] Getting user location every n minutes after app goes to background
问题描述
我试图实施<给定的建议href=\"http://stackoverflow.com/questions/6347503/how-do-i-get-a-background-location-update-every-n-minutes-in-my-ios-application\">this帖子。
不幸的是,步骤我也不清楚。我试图执行这些建议,但backgroundTimeRemaining继续即使我开始和停止locationServices减少。这是我开发的:
- (无效)applicationDidEnterBackground:(UIApplication的*){应用 UIApplication的*应用= [UIApplication的sharedApplication] bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
[应用endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}]; //开始长时间运行的任务,并立即返回。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { //完成与任务相关的工作。
self.timer =零;
[个体经营initTimer] });
}
initTimer:
- (无效){initTimer //创建位置管理器,如果此对象不
//已经有一个。
如果(零== self.locationManager)
self.locationManager = [[CLLocationManager的alloc]初始化]; self.locationManager.delegate =自我;
[self.locationManager startMonitoringSignificantLocationChanges] 如果(self.timer ==无){
self.timer = [的NSTimer scheduledTimerWithTimeInterval:0.3
目标:自我
选择:@选择(checkUpdates :)
用户信息:无
重复:YES];
}
}
checkUpdates:
- (无效)checkUpdates:(*的NSTimer)定时器{
UIApplication的*应用= [UIApplication的sharedApplication]
双剩余= app.backgroundTimeRemaining;
如果(剩余&LT; 580.0){
[self.locationManager startUpdatingLocation]
[self.locationManager stopUpdatingLocation]
[self.locationManager startMonitoringSignificantLocationChanges]
}
DbgLog(@Reminaing%F,app.backgroundTimeRemaining);
}
有没有人有什么可能是错误的,我code有何建议?无论initTimer和checkUpdates都被称为,但仅仅是在为背景执行时间(+ - 10分钟)。我希望应用程序每n分钟永远更新的位置。
我的应用程序的UIBackgroundModes设置为位置。
更新:
我现在重新上didUpdateToLocation和didFailWithError计时器。但仍backgroundTimeRemaining不断下降:
- (空)的LocationManager:(CLLocationManager *)经理
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {的NSLog(@难道更新位置=%F /%F,[newLocation坐标] .latitude,[newLocation坐标] .longitude);UIApplication的*应用= [UIApplication的sharedApplication]bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
[应用endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];//开始长时间运行的任务,并立即返回。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { //完成与任务相关的工作。 [个体经营initTimer]});
} - (无效)的LocationManager:(CLLocationManager *)经理didFailWithError:(NSError *)错误{[self.locationManager stopUpdatingLocation]
UIApplication的*应用= [UIApplication的sharedApplication]bgTask = [应用beginBackgroundTaskWithExpirationHandler:^ {
[应用endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];//开始长时间运行的任务,并立即返回。
[个体经营initTimer]
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { //完成与任务相关的工作。});}
我也是无效的计时器:
- (无效)checkUpdates:(*的NSTimer)定时器{
UIApplication的*应用= [UIApplication的sharedApplication]
双剩余= app.backgroundTimeRemaining;
如果(剩余&LT;&580.0功放;&安培;其余&GT; 570.0){
[self.timer无效]
self.timer =零;
[self.locationManager startUpdatingLocation]
[self.locationManager stopUpdatingLocation]
}
DbgLog(@********检查更新!!!!!!!!!!! Reminaing%F应用程序。 backgroundTimeRemaining);
}
几天以后,尝试所有可能的解决方案我终于能够使它发挥作用。下面是什么是错在我的解决方案:
- 我需要设置2 UIBackgroundTaskIdentifier,一个计时器,另一个用于的LocationManager
在我的解决方案,只需添加:
的UIApplication *应用= [UIApplication的sharedApplication]bgTask2 = [应用beginBackgroundTaskWithExpirationHandler:^ {[应用endBackgroundTask:bgTask2]; bgTask2 = UIBackgroundTaskInvalid; }];self.locationManager.delegate =自我;
[self.locationManager startUpdatingLocation]
I am trying to implement the suggestions given in this post.
Unfortunately the steps are not clear to me. I tried implementing those suggestions, but the backgroundTimeRemaining continues to decrease even after I start and stop locationServices. This is how I developed it:
- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
self.timer = nil;
[self initTimer];
});
}
initTimer:
- (void)initTimer {
// Create the location manager if this object does not
// already have one.
if (nil == self.locationManager)
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
if (self.timer == nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.3
target:self
selector:@selector(checkUpdates:)
userInfo:nil
repeats:YES];
}
}
checkUpdates:
- (void)checkUpdates:(NSTimer *)timer{
UIApplication* app = [UIApplication sharedApplication];
double remaining = app.backgroundTimeRemaining;
if(remaining < 580.0) {
[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
}
DbgLog(@"Reminaing %f", app.backgroundTimeRemaining);
}
Does anyone have a suggestion on what might be wrong in my code? Both initTimer and checkUpdates are being called, but only during for the background execution time (+- 10 Mins). I want the app to update the location every n minutes "forever".
My app's UIBackgroundModes is set to location.
UPDATE:
I am now resetting the timer on didUpdateToLocation and didFailWithError. But still the backgroundTimeRemaining keeps decreasing:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"Did Update Location = %f / %f", [newLocation coordinate].latitude, [newLocation coordinate].longitude);
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
[self initTimer];
});
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[self.locationManager stopUpdatingLocation];
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
[self initTimer];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
});
}
I am also invalidating the timer:
- (void)checkUpdates:(NSTimer *)timer{
UIApplication* app = [UIApplication sharedApplication];
double remaining = app.backgroundTimeRemaining;
if(remaining < 580.0 && remaining > 570.0) {
[self.timer invalidate];
self.timer = nil;
[self.locationManager startUpdatingLocation];
[self.locationManager stopUpdatingLocation];
}
DbgLog(@"*************************Checking for updates!!!!!!!!!!! Reminaing %f", app.backgroundTimeRemaining);
}
After some days trying all possible solutions I was finally able to make it work. Here is what was wrong in my solution:
- I need to setup 2 UIBackgroundTaskIdentifier, one for the timer, and another one for the locationManager
On my solution, just add:
UIApplication *app = [UIApplication sharedApplication];
bgTask2 = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask2]; bgTask2 = UIBackgroundTaskInvalid; }];
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
这篇关于获取用户位置每n分钟应用被切换到后台后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!