iOS应用程序背景下载 [英] iOS Application Background Downloading

查看:178
本文介绍了iOS应用程序背景下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喂!我需要知道如何让我的iOS应用程序在应用程序的后台启动下载(如下载在AppDelegate文件中运行),因此更改ViewControllers不会中断或取消下载。我还需要能够获得下载的进度( 0.00000 - 1.00000 ),设置一个 UIProgressView 对象to,这也意味着我需要一个 - (void)progressDidChangeTo:(int)progress function。

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.

推荐答案

只需使用 ASIHTTPRequest ,它比NSURLRequest更容易,并且完全符合您的要求需要。
示例显示如何在后台下载和如何报告进度。

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.

我不会直接在AppDelegate中下载任何内容。相反,我将为此目的创建一个分离的类。让我们称之为 MyService 然后我将在我的应用程序委托中初始化该类。

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.

MyService 类中,我将添加ASINetworkQueue和少量准备好处理请求的方法。以下是您可以使用的ASI示例的代码:

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];
}

如果需要设置进度条。我将在MyService类中公开ASINetworkQueue的setDownloadProgressDelegate,并将其设置在我的ViewController中:

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。如果您需要继续下载,即使您的应用程序退出,您可以将 ShouldContinueWhenAppEntersBackground 属性设置为YES。

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

这篇关于iOS应用程序背景下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆