iOS 应用程序在后台执行任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5323634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS application executing tasks in background
提问by Qamar Suleiman
I was wondering if I could send some webservice calls while my application is in the background. How does skype do it? Even if I press the home button my call stays connected.
我想知道是否可以在我的应用程序在后台时发送一些网络服务调用。skype是怎么做到的?即使我按下主页按钮,我的电话也会保持连接。
回答by Nathan Jones
Building on what rckoenes stated, applications are allowed to register background tasks to be completed after the user hits the home button. There is a time limit of 10 or 15 minutes for these tasks to complete. Again, you can register a task to complete immediately after the user hits home, this does NOT allow you to execute code say an hour after they exit the app.
根据 rckoenes 的说法,允许应用程序注册要在用户点击主页按钮后完成的后台任务。完成这些任务有 10 或 15 分钟的时间限制。同样,您可以在用户回家后立即注册要完成的任务,这不允许您在他们退出应用程序一小时后执行代码。
UIApplication* app = [UIApplication sharedApplication];
task = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task.
NSLog(@"Started background task timeremaining = %f", [app backgroundTimeRemaining]);
if (connectedToNetwork) {
// do work son...
}
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
});
UPDATE: if your app supports versions of iOS previous to iOs 4, you should also check to ensure that multitasking is supported before registering a background task. Use something along the lines of:
更新:如果您的应用程序支持 iOs 4 之前的 iOS 版本,您还应该检查以确保在注册后台任务之前支持多任务处理。使用以下内容:
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;
回答by Gagan_iOS
Try This... Excellent code for running app in background with no time limit. (I tested it for downloading more than 600 mb data from web-service.)
试试这个......在没有时间限制的情况下在后台运行应用程序的优秀代码。(我测试了它从网络服务下载超过 600 mb 的数据。)
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
}
Update ::
更新 ::
you can found more information regarding multitaksing in this apple doc Background Execution.
Please test on device.
请在设备上测试。
回答by halbano
It depends or what kind of application are you trying to code. Skype is registered as a VoIP (Long-running app) app and this is why it can stay "running" although it is on the background.
这取决于您要编码的应用程序类型。Skype 已注册为 VoIP(长时间运行的应用程序)应用程序,这就是它可以保持“运行”状态的原因,尽管它在后台运行。
Apple separates apps in three:
Apple 将应用程序分为三部分:
- Executing Finite-Length Tasks (you can run tasks for a finite amount of time)
- Downloading Content in the Background (you can download content to present it to the user when the app becomes active again)
Implementing Long-Running Tasks (This is the most interesting background apps category, with some subcategories that the developer should define for your app)
- Apps that play audible content to the user while in the background, such as a music player app
- Apps that record audio content while in the background
- Apps that keep users informed of their location at all times, such as a navigation app
- Apps that support Voice over Internet Protocol (VoIP) (SKYPE is here)
- Apps that need to download and process new content regularly
- Apps that receive regular updates from external accessories
- 执行有限长度任务(您可以在有限的时间内运行任务)
- 在后台下载内容(您可以下载内容以在应用再次激活时将其呈现给用户)
实现长时间运行的任务(这是最有趣的后台应用程序类别,开发人员应该为您的应用程序定义一些子类别)
- 在后台向用户播放可听内容的应用程序,例如音乐播放器应用程序
- 在后台录制音频内容的应用
- 让用户随时了解其位置的应用程序,例如导航应用程序
- 支持互联网协议语音 (VoIP) 的应用程序(SKYPE 在这里)
- 需要定期下载和处理新内容的应用
- 从外部配件接收定期更新的应用程序
So, you need to evaluate in which category your app is and what your service operation performs. Maybe if you're sending some small things to the service the best approach is only to request some extra time on the background for doing the job.
因此,您需要评估您的应用属于哪个类别以及您的服务操作执行的操作。也许如果您要向服务发送一些小东西,最好的方法就是在后台请求一些额外的时间来完成这项工作。
More info about all of this are on this link:
关于所有这些的更多信息在这个链接上: