Date un'occhiata a questa pagina, Apple descrive come mantenere l'applicazione iOS non sospesa mentre è in background. Apple Documentation
E qui è quello che ho implementato:
UIBackgroundTaskIdentifier bgTask;
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
NSLog(@"=== DID ENTER BACKGROUND ===");
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
// [self askToRunMoreBackgroundTask]; This code seems to be unnecessary. I'll verify it.
}];
if (bgTask == UIBackgroundTaskInvalid) {
NSLog(@"This application does not support background mode");
} else {
//if application supports background mode, we'll see this log.
NSLog(@"Application will continue to run in background");
}
}
/*
- (void)askToRunMoreBackgroundTask
{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
NSLog(@"restart background long-running task");
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"End of tolerate time. Application should be suspended now if we do not ask more 'tolerance'");
[self askToRunMoreBackgroundTask];
}];
}
*/
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"=== GOING BACK FROM IDLE MODE ===");
//it’s important to stop background task when we do not need it anymore
[[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
}
fonte
2014-10-24 08:33:58
cosa fa il metodo [self endBackgroundTask]? Cos'è il sé? – ozgur
@ozgurv: codice aggiornato. grazie – Vardhan