【发布时间】:2011-08-03 08:45:25
【问题描述】:
我想在应用程序在后台运行几分钟后终止它。
这样的步骤:
按下主页按钮,然后应用程序在后台运行。 2 分钟后应用程序自动终止。
我找到了如下方法:
- (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;
});
}
如你所见, 我想在那个地方添加一个计时器。
但是计时器在后台没有工作。我也尝试了 performSelector:afterdelay:
我该怎么做?
【问题讨论】:
标签: iphone objective-c ios