8

Hey! I need to know how I can have my iOS Application start a download in the background of the application (like, have the download run in the AppDelegate file) so changing ViewControllers will not interrupt or cancel the download. I also need to be able to get the progress of the download (0.00000 - 1.00000), to set a UIProgressView object to, which also means I need a - (void)progressDidChangeTo:(int)progress function.

2 Answers 2

18

Just use ASIHTTPRequest it is way easier than NSURLRequest and does exactly what you need. It examples that shows how to download in background and how to report progress.

I wouldn't download anything in the AppDelegate directly. Instead I would create a separated class just for that purpose. Let's call it MyService I would then initialize that class in my app delegate.

The class can work as a singleton or can be passed to each view controller that requires it.

In MyService class I would add the ASINetworkQueue and few methods to handle the requests when they are ready. Here is the code from ASI examples that you can use:

- (IBAction)startBackgroundDownloading:(id)sender
{
   if (!self.queue) {
      self.queue = [[[ASINetworkQueue alloc] init] autorelease];
   }

   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request setDidFinishSelector:@selector(requestDone:)];
   [request setDidFailSelector:@selector(requestWentWrong:)];
   [self.queue addOperation:request]; //queue is an NSOperationQueue
   [self.queue go];
}

- (void)requestDone:(ASIHTTPRequest *)request
{
   NSString *response = [request responseString];
   //Do something useful with the content of that request.
}

- (void)requestWentWrong:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

If you need to set the progress bar. I would just expose the setDownloadProgressDelegate of ASINetworkQueue in my MyService class and set it in my ViewControllers like that:

[[MyService service] setDownloadProgressDelegate: self.myUIProgressView];

BTW. If you need to continue downloading even when your app exits you can set ShouldContinueWhenAppEntersBackground property of your request to YES.

2
  • ... I am. I need the download to occur in the AppDelegate file, so that the download will not stop when the UIViewController that initiated it starts. I need to know how I can get a UIViewController to start and access the ASIHTTPRequest object, but have the object held in the AppDelegate. Jan 2, 2011 at 19:06
  • The problem is the reportFailure will be called in ASIHTTPRequest.m everytime when my app is coming back from background. I'm using the same way as you suggested in your post and the ShouldContineWhenAppEntersBackground set to YES.
    – Bagusflyer
    Oct 17, 2012 at 3:50
3

you can use NSURLConnection to start an asynchronous request that won't cause your UI to be frozen. You can do it by doing something like:

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
connection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[urlRequest release];

in order to have your progress you can use the:

connection:didReceiveResponse:(NSURLResponse *)response;

delegate call to inspect the response.expectedContentLength and then use the

connection:didReceiveData:(NSData *)data

to track the amount of data that was downloaded and calculate a percentage.

Hope this helps, Moszi

2
  • I have the downloading part down, using ASIHTTPRequest, I just need to know how to implement the download to start in the background, rather than the current open UIViewController, so when the view controller is dismissed, the download continues. Jan 2, 2011 at 19:03
  • You can create a singleton class for managing your download, this way this class will be around even if your view controller is dealloc-ed. (singleton pattern on wikipedia: en.wikipedia.org/wiki/Singleton_pattern )
    – Moszi
    Jan 2, 2011 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.