我们有一个应用程序,该应用程序具有需要退出的特定用途。退出后,进程需要在后台运行一段时间或直到完成。我们只需要知道如何以编程方式强制应用程序进入后台可以继续运行的背景。任何帮助都会很棒!提前致谢!强制关闭应用程序并在后台运行
更新:我们已确认,似乎没有强制应用程序退出/进入后台并继续运行后台任务的程序化方式。您可以使用exit(0)强制应用程序退出;但这会一起杀死应用程序。但是,这个问题的大部分都涉及到后台运行任务。我们找到了一种解决方案,可让我们的应用程序开始处理数据并处理用户已设置好的待处理任务。这是所需的代码。这需要添加到应用程序委托中,并且需要在设备/ 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
});
}
}
}
}
对于那些寻找解决方案的人,可以使用私有API,[请参阅此答案](http:// stackoverflow。 COM /一个/119114分之15997772)。 – Nate