【发布时间】:2013-03-26 04:50:23
【问题描述】:
我有一个使用 ASIHTTP 请求运行的下载队列。当用户按下主屏幕并且应用程序进入后台时,我希望此操作继续。我知道 ASIHTTP 请求可以在后台运行,但我希望运行 ASIHTTP 请求的进程也可以在后台运行。
我该怎么做?
我在 StackOverflow 看到了这个帖子:Continuing a long running process in the background under iOS4
但解决方案是在 iOS4 中。我想在 iOS5 及更高版本中执行此操作..
给出的解决方案:
UIApplication *app = [UIApplication sharedApplication];
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (backgroundTaskIdentifier != UIBackgroundTaskInvalid)
{
// you took too long - clean up what you can, then …
[app endBackgroundTask:backgroundTaskIdentifier];
backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
});
}];
}
另一个问题是我应该把这段代码放在哪里。
在这方面需要一些指导..
【问题讨论】:
标签: ios ios5 ios6 background-process