我想在几分钟后终止应用程序,而它在后台运行

时间:2023-02-04 19:17:01

i want to terminate the app after some minutes while it runs in background.

我想在几分钟后终止应用程序,而它在后台运行。

steps like these :

这些步骤:

press the home button then app run in background. 2minutes later the app terminates automaticly.

按主页按钮然后应用程序在后台运行。 2分钟后,应用程序自动终止。

i find a method as below:

我找到一个方法如下:

- (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.

           //============================
          // here  i want to add a timer  
          //=============================  

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

as you see, i want to add a timer in that place.

如你所见,我想在那个地方添加一个计时器。

but the timer didn't work in background. i also tried the performSelector:afterdelay:

但计时器在后台不起作用。我也试过performSelector:afterdelay:

how can i do this?

我怎样才能做到这一点?

2 个解决方案

#1


1  

After 10 minutes, All app terminates.

10分钟后,所有应用程序终止。

If you have to exit app only after 2 mins,

如果你必须在2分钟后退出app,

set timer in (applicationWillResignActive)

设置计时器(applicationWillResignActive)

#2


0  

I suppose your app is doing some kind of work while in background - otherwise there would be no need to run it in the background in the first place.

我想你的应用程序在后台进行某种工作 - 否则就不需要在后台运行它了。

I further suppose you have some kind of runloop going, since you'd like to terminate after a certain amount of time and not after some task has been completed.

我进一步假设你有某种类型的runloop,因为你想在一段时间之后终止,而不是在完成一些任务之后终止。

So I'd suggest logging the system time at the moment the app goes into background and then in your runloop check for the current system time once per iteration. If it is greater than the initial time + 2 minutes, terminate background activity.

因此,我建议在应用程序进入后台时记录系统时间,然后在每次迭代时在runloop中检查当前系统时间。如果它大于初始时间+ 2分钟,则终止后台活动。

#1


1  

After 10 minutes, All app terminates.

10分钟后,所有应用程序终止。

If you have to exit app only after 2 mins,

如果你必须在2分钟后退出app,

set timer in (applicationWillResignActive)

设置计时器(applicationWillResignActive)

#2


0  

I suppose your app is doing some kind of work while in background - otherwise there would be no need to run it in the background in the first place.

我想你的应用程序在后台进行某种工作 - 否则就不需要在后台运行它了。

I further suppose you have some kind of runloop going, since you'd like to terminate after a certain amount of time and not after some task has been completed.

我进一步假设你有某种类型的runloop,因为你想在一段时间之后终止,而不是在完成一些任务之后终止。

So I'd suggest logging the system time at the moment the app goes into background and then in your runloop check for the current system time once per iteration. If it is greater than the initial time + 2 minutes, terminate background activity.

因此,我建议在应用程序进入后台时记录系统时间,然后在每次迭代时在runloop中检查当前系统时间。如果它大于初始时间+ 2分钟,则终止后台活动。