Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

iphone - Force app to close and run in background

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(@"

Process background tasks!

");

                    // Do your work here

                });
            }
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...