当应用程序进入后台时,应用程序委托没有调用



我正在实现一个iPhone应用程序。我在应用程序中使用异步请求从服务器下载数据。如果我按下home键,我的应用程序委托没有直接调用。从服务器下载数据完成后,应用程序委托方法正在调用。那么,我如何在我的应用程序中获得即时委托调用呢?

您正在实现哪些委托方法?你能把你尝试过的代码贴在这里吗?注意,一旦你的应用程序被移到后台,你只能运行短期任务以节省电池寿命。

这个例子由Apple提供:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        // Clean up any unfinished task business by marking where you.
        // stopped or ending the task outright.
        [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, preferably in chunks.
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

如果您想执行长时间运行的任务,则需要特殊权限,即使这样也只能执行特定子集的操作。

iOS App Programming Guide: App States and Multitasking

相关内容

最新更新