I need help on how to keep in-app-purchase items in my application downloading when the user sleeps his iPhone, or the application enter the background mode. Does anyone have any ideas?
Define BackgroundTaskWithExpirationHandler in applicationDidEnterBackground: like this:
// ...
UIBackgroundTaskIdentifier bti;
// ...
- (void)applicationDidEnterBackground:(UIApplication *)application {
bti = [application beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if(bti != UIBackgroundTaskInvalid) {
[application endBackgroundTask:bti];
bti = UIBackgroundTaskInvalid;
}
});
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// DO or CONTINUE RESUMED DOWNLOAD HERE
dispatch_async(dispatch_get_main_queue(), ^{
if(bti != UIBackgroundTaskInvalid) {
[application endBackgroundTask:bti];
bti = UIBackgroundTaskInvalid;
}
});
});
}
To see how it works download sample project I made for You.
git clone https://github.com/jacekmigacz/BTWEH.git
...and press Home Button to trigger applicationDidEnterBackground:.
Idea is to delegate your download task to another, privileged thread and keep your application in background state instead of let OS suspend it.