强制app关闭并在后台运行 [英] Force app to close and run in background

查看:403
本文介绍了强制app关闭并在后台运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个具有特定用途的应用,需要退出。退出后,流程需要在后台运行一段时间或直到完成。我们只需要知道如何以编程方式强制应用程序进入进程可以继续运行的后台。对此的任何帮助都会很棒!提前致谢!

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!

更新:我们已经确认似乎没有一种程序化的方法来强制应用退出/进入后台并继续运行后台任务。您可以使用exit(0)强制应用程序退出;但这会将应用程序全部杀死。但是,这个问题的大部分内容都与后台运行任务有关。我们找到了一个解决方案,允许我们的应用程序开始处理用户已设置处理的数据和处理任务。这是所需的代码。这需要添加到app delegate并且设备/ IOS上需要多任务处理。

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

                });
            }
        }
    }
}


推荐答案

您可以通过将注册的URL发送到另一个应用程序来强制iOS应用程序进入后台,例如Safari的网站URL。

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 ]];

许多叫Safari处理网址的应用都是Apple认可的。

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

要在后台获得任何时间,必须使用允许的背景模式之一(音频,位置或VOIP)正确配置应用程序;或者应用程序可以通过调用beginBackgroundTaskWithExpirationHandler方法在挂起之前请求几分钟的额外时间

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

这篇关于强制app关闭并在后台运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆