12

I am creating an application wherein I am downloading some data from server. While going in background I want that connection should continue running so that data can be downloaded. I know there is method in appDelegate

- (void)applicationDidEnterBackground:(UIApplication *)application  

which is called when application enters background. But as the connection is created in viewController, how can it be managed in appDelegate?
Also is/are there other way(s) this can be done? I have gone through this link but is there a something simple to implement?

mfaani
  • 33,269
  • 19
  • 164
  • 293
Nitish
  • 13,845
  • 28
  • 135
  • 263

3 Answers3

15

One way to do some operations that continue in the background is to create a separate thread to do the downloading. Inside the thread, bracket your download operations between calls to beginBackgroundTaskWithExpirationHandler: and endBackgroundTask. You don't need to check to see whether you are running in the background or not, you just always call these two methods.

// Tell iOS this as a background task in case we get backgrounded
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] 
          beginBackgroundTaskWithExpirationHandler:NULL];

//----------------------------------------------
// Perform your download operations here
//----------------------------------------------
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

// Tell iOS that we are done with stuff that needed to keep going even if backgrounded    
[[UIApplication sharedApplication] endBackgroundTask:taskId];
progrmr
  • 75,956
  • 16
  • 112
  • 147
  • 1
    You can set ASIHttpRequest property : [request setShouldContinueWhenAppEntersBackground:YES]; – HelloWorld Dec 08 '12 at 08:03
  • Thank you so much!!! Please correct me if I'm wrong: To conclude, there's almost no difference between running the task in background or foreground. Both would execute with this code. Yet 2 differences come to mind: 1. **if** your background task is too long, then maybe you would get terminated before finishing it! 2. It's better to `endBackgroundTask:taskId` so you'd save extra battery. – mfaani Jul 19 '17 at 18:45
3

[edit] Sorry I was incorrect, as was pointed out in the comments you can extend the time limit you have to perform operations once/before your app goes into the background. Here is Apple's Official Documentation

bennythemink
  • 5,096
  • 4
  • 36
  • 54
  • This is not true, you can execute a [Finite-Length task](http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW28) in the background for a maximum of 10 min. – rckoenes Jun 19 '12 at 12:31
  • @rckoenes you are indeed correct, my apologies. I will edit my answer. Thanks for the information :) – bennythemink Jun 19 '12 at 12:37
  • Still not true, you can call [`beginBackgroundTaskWithExpirationHandler:`](http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:) before executing the Finite-Length task. You do not need to perform the operations once you go in the background. You can start the task before your app get pushed to background. – rckoenes Jun 19 '12 at 12:43
1

I don't know how you handle your data downloading exactly. But you can take a look at ASIHTTPRequest. It is very simple and straightforward, and works with ARC if you set the compiler flags to -fno-objc-arc. With this you only have to use

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setShouldContinueWhenAppEntersBackground:YES]; //For iOS 4.0 and up 

And that works.

Here you can see how ASIHTTPRequest works

Hope it helps!

Thermometer
  • 2,567
  • 3
  • 20
  • 41
  • ASIHTTPRequest is not actively developed anymore... as per message [here](http://allseeing-i.com/ASIHTTPRequest/). Just saying. – zero0cool Jun 28 '12 at 15:49
  • True, but it still works like a charm. I haven't found any good alternatives with as much functionality as ASIHTTPRequest. – Thermometer Jun 29 '12 at 12:51