使用afnetworking一次下载一个文件 [英] Download one file at a time using afnetworking

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

问题描述

我有一个数组,其中包含不同的URL。我要下载一个带有进度条的文件,而不是开始下一个文件,依此类推。

I have an array which contains different URLs. I want to download one file with a progress bar, than start the next one and so on.

这是我到目前为止的代码;

Here is my code that I have so far;

-(void) func:(NSArray *) arry
{

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    configuration.timeoutIntervalForRequest = 900;
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    NSMutableArray * downloadedUrl = [[NSMutableArray alloc] init];
    for (NSString * str in arry) {
        NSURL *URL = [NSURL URLWithString:str];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
             NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

             return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
        } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

            if(error)
            {
                NSLog(@"File Not Dowloaded %@",error);

            }

        }];
        [downloadTask resume];
    }
}

您如何一次下载一个文件?进度条,然后从数组中删除URL?

How would you download one file at a time with a progress bar and then remove the url from array?

推荐答案

声明一个全局 NSMutableArray

Declare one global NSMutableArray of file and used that in the function like below.

-(void) downloadFile {

     NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
     configuration.timeoutIntervalForRequest = 900;
     AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; 

     NSURL *URL = [NSURL URLWithString:[self.fileArr firstObject]]; //Here self.fileArr is your global mutable array
     NSURLRequest *request = [NSURLRequest requestWithURL:URL];

     NSURLSessionDownloadTask downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL (NSURL targetPath, NSURLResponse response) {
     NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];

        return [tmpDirURL URLByAppendingPathComponent:[response suggestedFilename]];
     } completionHandler:^(NSURLResponse response, NSURL filePath, NSError *error) {

        if(error)
        {
            NSLog(@"File Not Dowloaded %@",error);
            [self downloadFile];
        }
        else {
            [self.fileArr removeObjectAtIndex:0];
            if (self.fileArr.count > 0) {
                [self downloadFile];
            }
        }
    }];
    [downloadTask resume];
}

现在调用此函数,但在此之前初始化 self.fileArr,然后调用 downloadFile 方法。

Now call this function but before that initialize self.fileArr and after that call downloadFile method.

希望这对您有所帮助。

这篇关于使用afnetworking一次下载一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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