如何在我的应用程序进入后台模式时更新位置管理器和计时器?

时间:2020-12-10 01:26:10

I have a timer which shows the NSTime of a user's work out. Location manager and that timer stops updating when my app goes to background mode. How can i make them updating when my app is in background mode?

我有一个计时器,显示用户锻炼的NSTime。当我的应用程序进入后台模式时,位置管理器和该计时器停止更新。当我的应用处于后台模式时,如何让它们更新?

I have a view called RunViewController which has start button. When user clicks that button, timer and location manager starts. Code is:

我有一个名为RunViewController的视图,它有一个启动按钮。当用户单击该按钮时,计时器和位置管理器将启动。代码是:

 -(void)startRun{
    timeSec = 0;
    timeMin = 0;
    timeHour = 0;

    NSString* timeNow = [NSString stringWithFormat:@"%02d:%02d:%02d",timeHour , timeMin, timeSec];
    //Display on your label
    lblTime.text = timeNow;

    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

    self.locManager = [[CLLocationManager alloc] init];
    locManager.delegate = self;
    locManager.desiredAccuracy = kCLLocationAccuracyBest; //kCLLocationAccuracyBest
    [locManager setDistanceFilter:3]; //kCLDistanceFilterNone

    [locManager startUpdatingLocation];
    locationManagerStartDate = [[NSDate date] retain];


}

In - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

simply draws a line on map from old location to new location and save that location in an array.

只需在地图上从旧位置绘制一条线到新位置,然后将该位置保存在数组中。

Thanks in advance.

提前致谢。

2 个解决方案

#1


1  

Found a solution to implement this with the help of the Apple Developer Forums. I did the following:

在Apple Developer论坛的帮助下找到了实现此解决方案的解决方案。我做了以下事情:

  • Specify location background mode

    指定位置背景模式

  • Use an NSTimer in the background by using

    使用后台使用NSTimer

  • UIApplication:beginBackgroundTaskWithExpirationHandler: In case n is

    UIApplication:beginBackgroundTaskWithExpirationHandler:如果是n

smaller than UIApplication:backgroundTimeRemaining it does works just fine, in case n is larger, the location manager should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed. This does work since location is one of the three allowed types of background execution.

小于UIApplication:backgroundTimeRemaining它确实工作正常,如果n更大,应该在没有剩余时间之前再次启用(和禁用)位置管理器,以避免后台任务被杀死。这确实有效,因为location是三种允许的后台执行类型之一。

Note: Did loose some time by testing this in the simulator where it doesn't work, works fine on my phone.

注意:通过在无法运行的模拟器中进行测试可以节省一些时间,在我的手机上正常工作。

Here is the reference for the same.

这是相同的参考。

#2


0  

Try this..

-(void)viewDidLoad
{
    UIDevice* device = [UIDevice currentDevice];
    BOOL backgroundSupported = NO;
    if ([device respondsToSelector:@selector(isMultitaskingSupported)])
        backgroundSupported = device.multitaskingSupported;

    NSLog(backgroundSupported ? @"Yes" : @"No");


    counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
     //called after 10min of your app in background
        NSLog(@"10 minutes finished.");
    }];

    theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(selectorForUpdatingLocation) userInfo:nil repeats:YES];

}

#1


1  

Found a solution to implement this with the help of the Apple Developer Forums. I did the following:

在Apple Developer论坛的帮助下找到了实现此解决方案的解决方案。我做了以下事情:

  • Specify location background mode

    指定位置背景模式

  • Use an NSTimer in the background by using

    使用后台使用NSTimer

  • UIApplication:beginBackgroundTaskWithExpirationHandler: In case n is

    UIApplication:beginBackgroundTaskWithExpirationHandler:如果是n

smaller than UIApplication:backgroundTimeRemaining it does works just fine, in case n is larger, the location manager should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed. This does work since location is one of the three allowed types of background execution.

小于UIApplication:backgroundTimeRemaining它确实工作正常,如果n更大,应该在没有剩余时间之前再次启用(和禁用)位置管理器,以避免后台任务被杀死。这确实有效,因为location是三种允许的后台执行类型之一。

Note: Did loose some time by testing this in the simulator where it doesn't work, works fine on my phone.

注意:通过在无法运行的模拟器中进行测试可以节省一些时间,在我的手机上正常工作。

Here is the reference for the same.

这是相同的参考。

#2


0  

Try this..

-(void)viewDidLoad
{
    UIDevice* device = [UIDevice currentDevice];
    BOOL backgroundSupported = NO;
    if ([device respondsToSelector:@selector(isMultitaskingSupported)])
        backgroundSupported = device.multitaskingSupported;

    NSLog(backgroundSupported ? @"Yes" : @"No");


    counterTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
     //called after 10min of your app in background
        NSLog(@"10 minutes finished.");
    }];

    theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(selectorForUpdatingLocation) userInfo:nil repeats:YES];

}