我想在后台运行几分钟后终止该应用程序 [英] i want to terminate the app after some minutes while it run in background

查看:76
本文介绍了我想在后台运行几分钟后终止该应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在后台运行几分钟后终止该应用程序.

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

这样的步骤:

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

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

我找到一种如下的方法:

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.

,但计时器在后台不起作用.我也尝试了performSelector:afterdelay:

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

我该怎么做?

推荐答案

10分钟后,所有应用都会终止.

After 10 minutes, All app terminates.

如果您只需要在2分钟后退出应用程序,

If you have to exit app only after 2 mins,

在(applicationWillResignActive)中设置计时器

set timer in (applicationWillResignActive)

这篇关于我想在后台运行几分钟后终止该应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆