Han*_*kke 8 iphone download download-manager uiapplicationdelegate
嘿! 我需要知道如何让我的iOS应用程序在应用程序的后台开始下载(比如,在AppDelegate文件中运行下载),因此更改ViewControllers不会中断或取消下载.我还需要能够获得download(0.00000 - 1.00000)的进度,将UIProgressView对象设置为,这也意味着我需要一个- (void)progressDidChangeTo:(int)progress函数.
Pio*_*pla 18
只需使用ASIHTTPRequest它比NSURLRequest更容易,并完全满足您的需求.它的示例显示了如何在后台下载以及如何报告进度.
我不会直接在AppDelegate中下载任何内容.相反,我会为此目的创建一个单独的类.让我们调用它MyService然后我会在我的app委托中初始化该类.
该类可以作为单例工作,也可以传递给需要它的每个视图控制器.
在MyService类I中,我们将添加ASINetworkQueue和几个方法来处理它们准备好的请求.以下是您可以使用的ASI示例代码:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
如果需要设置进度条.我只是在我的MyService类中公开ASINetworkQueue的setDownloadProgressDelegate,并在我的ViewControllers中设置它:
[[MyService service] setDownloadProgressDelegate: self.myUIProgressView];
Run Code Online (Sandbox Code Playgroud)
BTW.如果您需要在应用程序退出时继续下载,则可以将ShouldContinueWhenAppEntersBackground请求的属性设置为YES.
| 归档时间: |
|
| 查看次数: |
28495 次 |
| 最近记录: |