We have an application that has a specific purpose, where an exit is required. After exiting, the process should run in the background for a certain time or until completion. We just need to know how to programmatically make the application enter the background, where processes can continue to work. Any help on this would be great! Thanks in advance!
UPDATE. We have confirmed that there seems to be a software way to get the application not to close / enter the background and not perform background tasks. You can force close the application using exit (0); but it kills the application all together. However, the main part of this question concerned the completion of tasks in the background. We found a solution that allows our application to begin processing data and processing tasks that the user has to process. Here is the code needed. This needs to be added to the application delegate, and multitasking is required on the device / iOS.
- (void)applicationDidEnterBackground:(UIApplication *)app{ // Check that IOS supports multitasking if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]){ // Check that the device supports multitasking if ([[UIDevice currentDevice] isMultitaskingSupported]) { // Custom setting to allow users the freedom to enable or disable background tasks BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"backgroundTasksEnabled_key"]; if ( enabled ){ //Get the shared application instance backGroundApp = [UIApplication sharedApplication]; background_task = [backGroundApp beginBackgroundTaskWithExpirationHandler: ^ { [backGroundApp endBackgroundTask: background_task]; background_task = UIBackgroundTaskInvalid; }]; // Run in background dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"\n\nProcess background tasks!\n\n"); // Do your work here }); } } } }
source share