iOS应用后台下载 [英] iOS Application Background Downloading

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

问题描述

嘿!我需要知道如何让我的 iOS 应用程序在应用程序的后台开始下载(例如,在 AppDelegate 文件中运行下载),以便更改 ViewControllers 不会中断或取消下载.我还需要能够获取下载进度 (0.00000 - 1.00000),以设置 UIProgressView 对象,这也意味着我需要一个 -(void)progressDidChangeTo:(int)progress 函数.

解决方案

只要使用 ASIHTTPRequest 就可以了比 NSURLRequest 更容易,并且完全满足您的需求.它示例显示了如何在后台下载以及如何报告进度.>

我不会直接在 AppDelegate 中下载任何东西.相反,我会为此目的创建一个单独的类.让我们称之为 MyService 然后我会在我的应用程序委托中初始化那个类.

该类可以作为单例工作,也可以传递给需要它的每个视图控制器.

MyService 类中,我将添加 ASINetworkQueue 和一些方法来在它们准备好时处理请求.以下是您可以使用的 ASI 示例中的代码:

- (IBAction)startBackgroundDownloading:(id)sender{如果(!self.queue){self.queue = [[[ASINetworkQueue alloc] init] autorelease];}NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[请求 setDelegate:self];[请求 setDidFinishSelector:@selector(requestDone:)];[请求 setDidFailSelector:@selector(requestWentWrong:)];[self.queue addOperation:request];//队列是一个NSOperationQueue[self.queue go];}- (void)requestDone:(ASIHTTPRequest *)request{NSString *response = [请求响应字符串];//对该请求的内容做一些有用的事情.}- (void)requestWentWrong:(ASIHTTPRequest *)request{NSError *error = [请求错误];}

如果需要设置进度条.我只会在 MyService 类中公开 ASINetworkQueue 的 setDownloadProgressDelegate 并将其设置在我的 ViewControllers 中,如下所示:

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

顺便说一句.如果您需要在应用退出时继续下载,您可以将请求的 ShouldContinueWhenAppEntersBackground 属性设置为 YES.

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.

解决方案

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.

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

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