By Qamar Suleiman


2011-03-16 10:00:27 8 Comments

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.

3 comments

@Gagan_iOS 2012-08-04 13:58:31

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.

@Salim 2013-10-07 10:50:18

I am using it but after 10 minutes task is ending.

@Salim 2014-01-15 05:28:58

This is not working iOS 4, 5, 6. working only for iOS 7

@Karun 2014-01-27 06:55:55

Super. Thank you so much for this. It really helped me a lot.

@Uygar Y 2014-02-07 09:57:27

Wow! It worked for me too... It also works on iOS 6 btw.

@Mr.G 2014-09-22 05:40:56

If i implement this to upload data to server . will the apple reject my app ??

@Gagan_iOS 2014-09-22 06:10:50

I my case Apple did not rejected my app & it is on App store.

@Mr.G 2014-09-22 06:36:14

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 ,

@Gagan_iOS 2014-09-22 08:31:24

If 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.

@Mr.G 2014-09-22 08:41:01

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

@kimbl 2015-10-24 11:19:24

will this also work for more than 10minute? I have to download more than 10Gb.

@Gagan_iOS 2015-10-26 09:42:57

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 developer.apple.com/library/ios/documentation/Foundation/… raywenderlich.com/51127/nsurlsession-tutorial NSURLSession is more convenient for background operations.

@dev_m 2016-09-28 12:55:44

@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 ...

@Nixta 2019-07-23 15:57:55

Except there is a time limit.

@halbano 2015-05-21 18:48:10

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:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

@Nathan Jones 2011-03-16 13:48:11

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;

@user3847320 2016-04-19 19:51:31

As of iOS 7 the background execution time was reduced from 10 minutes to 3 minutes.

@Ganesh Guturi 2017-08-09 13:21:43

@user3847320, any document please...

@user3847320 2017-08-10 14:30:34

@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.

Related Questions

Sponsored Content

28 Answered Questions

[SOLVED] How to change the background color of a UIButton while it's highlighted?

17 Answered Questions

[SOLVED] How do I call Objective-C code from Swift?

  • 2014-06-02 20:05:42
  • David Mulder
  • 269785 View
  • 927 Score
  • 17 Answer
  • Tags:   objective-c swift

42 Answered Questions

[SOLVED] How to check for an active Internet connection on iOS or macOS?

15 Answered Questions

2 Answered Questions

[SOLVED] Keep program running in background ios

10 Answered Questions

[SOLVED] Understanding REST: Verbs, error codes, and authentication

  • 2010-01-04 19:55:36
  • Pekka 웃
  • 129488 View
  • 587 Score
  • 10 Answer
  • Tags:   web-services rest

1 Answered Questions

[SOLVED] Terminate an application in IOS & Background mode

  • 2015-02-13 12:54:56
  • Rouki
  • 151 View
  • 0 Score
  • 1 Answer
  • Tags:   ios objective-c

1 Answered Questions

1 Answered Questions

Sponsored Content