我有一个使用 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;
}
});
}];
}
另一个问题是我应该把这段代码放在哪里。
需要一些指导。