3

we have an app that has a specific purpose where an exit is required. After the exit a process needs to run in the background for a certain amount of time or until finished. We just need to know how to programmatically force the app to enter the background where processes can continue running. Any help on this would be great! Thanks in advance!

UPDATE: We have confirmed that there does not seem to be a programmatic way to force the app to quit / enter background and continue running background tasks. You can force the the app to exit using exit(0); but this kills the app all together. However, the bulk of this question was concerning running tasks in the background. We have found a solution that allows our app to begin processing data and handling tasks that a user has setup to be processed. Here is the code required. This needs to be added to the app 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

                });
            }
        }
    }
}
mejim707
  • 431
  • 3
  • 16
  • For those searching for a solution, who can use private APIs, [see this answer](http://stackoverflow.com/a/15997772/119114). – Nate Apr 14 '13 at 10:02

3 Answers3

2

You can force an iOS app into the background by sending a registered URL to another app to handle, such as a web site URL to Safari.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: myWebsiteURL ]];

Many many apps that call Safari to handle URLs are approved by Apple.

To get any time in the background, an app has to be appropriately configured using one of the allowed background modes (audio, location or VOIP); or the app can request a few minutes of extra time in the background before being suspended by calling the beginBackgroundTaskWithExpirationHandler method

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • So, would you say that the above code I posted is an appropriate solution for handling perhaps 2 - 3 minutes worth of background processing? From what I understand this is an approved method by apple but maybe you could confirm? Thanks a lot. – mejim707 Apr 18 '12 at 16:06
  • wondering how Qubii works, seems it can launch in the background and backup the phone while charing. – Fu Jiantao Jun 12 '20 at 16:14
0

You can't have a process running (doing work) in the background in iOS, you get a few seconds when the app quits to do any clean up and that's it!

Matt Lacey
  • 8,227
  • 35
  • 58
-1

You cannot force an application into the background, I'm fairly sure that Apple's guidelines prohibit you from doing that. What could your app possibly be doing that it can only do in the background and not in the foreground?

Seventoes
  • 4,810
  • 3
  • 19
  • 19
  • Well, it's not that it can't do a process in the foreground and requires background, it's that we want the user to have the ability to do what they have to do and the app will handle the rest while they continue or with their work or play or whatever. Just real quick in and out without waiting for a process to complete. We have been seeing a lot where people say this is not a good idea to close the app or force into the back. As it stand I have the app run a process, gain confirmation after everything is complete then auto close itself. However, if the home button is hit the process pauses. TX – mejim707 Jan 19 '12 at 21:54
  • 1
    An app should never force-close itself. Respond to the user closing the application, don't cause it to happen on your own. Also take a look at Apple's multitasking guides, you should be able to specify a background process if needed. – Seventoes Jan 20 '12 at 05:17