我想在終點後幾分鐘後終止應用程序,而它在後臺運行。我想在幾分鐘後終止應用程序,而它在後臺運行
這些步驟如下:
按主頁按鈕,然後在後臺運行應用程序。 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:
我該怎麼做?
對不起
如果只有後2分鐘退出應用程序,
設置定時器。你的傾向是錯誤的......實際上在背景中沒有任何工作。我只是想要一個可以在後臺做一些工作的任務。但是,當應用程序返回時,什麼都不能工作。 –