iOS application executing tasks in background

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.


Solution 1:

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;

Solution 2:

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.

Solution 3:

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