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.
-
6If you read the Apple documentation on background running you will see that you are only allowed to run your app in background if, you are playing audio, monitoring location or your app is VOIP client. If your app does not fall in one of the above categories then you are out of luck. http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html – rckoenes Mar 16 '11 at 10:08
-
@rckoenes This is not true. You can get *some* CPU time for finite tasks *(usually around 10 minutes or so)*. See [the docs](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html) – webo80 Feb 17 '16 at 16:23
-
@webo80 true you can get some time, in iOS 9 it is way less then 10 min. But in general you can just do something task in the background. – rckoenes Feb 18 '16 at 10:39
-
1@rckoenes at least, you have a little time to finish what you're doing, on the worst scenario – webo80 Feb 18 '16 at 10:46
3 Answers
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.
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:
UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
backgroundSupported = device.multitaskingSupported;

- 64,370
- 15
- 118
- 145

- 1,836
- 14
- 13
-
As of iOS 7 the background execution time was reduced from 10 minutes to 3 minutes. – user3847320 Apr 19 '16 at 19:51
-
@GaneshGuturi No direct documentation, and Apple explicitly states the background time is allotted opportunistically, based on a number of variables. Trail and error, (by logging UIApplication.shared.backgroundTimeRemaining) indicates the 3 minute execution time. This of course could change at Apple's discretion. – user3847320 Aug 10 '17 at 14:30
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.)
- (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.

- 1,213
- 22
- 32

- 3,638
- 3
- 32
- 51
-
5
-
-
-
2If i implement this to upload data to server . will the apple reject my app ?? – Mr.G Sep 22 '14 at 05:40
-
-
Gagan_iOS i want to upload large videos to amazon S3 server . it works perfectly in foreground mode , but sometimes is upload more than 200 mb , – Mr.G Sep 22 '14 at 06:36
-
1If you are able to upload videos on server in background mode also then in my view your have done your job. In my case I performed more than 500 MB data upload on server in background mode and app is on app store. First time my app was rejected because of my fault(added SOUND background service in plist) but next time it was on App store. – Gagan_iOS Sep 22 '14 at 08:31
-
its good to hear , in my case it doesn't upload in background mode, can u share a set of codes for me to do this – Mr.G Sep 22 '14 at 08:41
-
will this also work for more than 10minute? I have to download more than 10Gb. – kimbl Oct 24 '15 at 11:19
-
This should work. But now I would suggest you to use NSURLSession for background download. It is more reliable & time saving. Below are few links on NSURLSession https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/ http://www.raywenderlich.com/51127/nsurlsession-tutorial NSURLSession is more convenient for background operations. – Gagan_iOS Oct 26 '15 at 09:42
-
@Gagan_iOS Can you please give explanation for this code, i mean for how much execution time it request to os, and will it continuously run my app forever ? i mean will it ask for more time again agin or only single time ... – dev_m Sep 28 '16 at 12:55
-
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.
Apple separates apps in three:
- 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
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:

- 1,135
- 14
- 34