我已经使用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;
}
});
}];
}另一个问题是我应该把这段代码放在哪里。
在这方面需要一些指导。
发布于 2013-03-26 13:56:30
你在另一个答案中找到的解决方案适用于iOS 4.0和更高版本。它工作得很好,当移动到后台时,它仍然保持the recommended way来执行扩展任务。
而且,除非您试图支持iOS 3.x --这已经不再那么可取了--否则您不需要respondsToSelector检查。
https://stackoverflow.com/questions/15629587
复制相似问题