如何使用AFNetwork下载多个文件

时间:2012-08-30 13:43:37

标签: objective-c ios afnetworking

我想使用AFNetwork下载多个文件,但我不知道如何实现这个? 如您所见,我创建了一个操作数组并在其中添加了3个任务

NSMutableArray *operations = [NSMutableArray array];
NSArray *requestArray = @[ @"...task1.zip", @"task2.zip", @"task3.zip" ];
for (int i = 0; i < 3; i++) {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[requestArray objectAtIndex:i]]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[[requestArray objectAtIndex:i] lastPathComponent]];
    operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
        NSLog(@"Operation%i: bytesRead: %d", i, bytesRead);
        NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesRead);
        NSLog(@"Operation%i: bytesRead: %lld", i, totalBytesExpectedToRead);
    }];
    [operations addObject:operation];
}

然后,我该怎么办?我做了一些事情,但没有任何反应

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];
[requestHandler enqueueBatchOfHTTPRequestOperations:operations progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {

} completionBlock:^(NSArray *operations) {
}];

关于这个问题的任何想法?

3 个答案:

答案 0 :(得分:5)

AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init];应替换为AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]]; 因为initWithBaseURL初始化了很多东西。

答案 1 :(得分:0)

[operation start];

或NSOperationQueue就是你要找的。

答案 2 :(得分:0)

试试这个......

NSArray *linkArr = [NSArray arrayWithObjects:@"http://abc.mp3",@"http://abc.mp3",@"http://abc.mp3", nil];

    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];

    for (int i=0; i< linkArr.count; i++) {

        NSString *urlpath = [linkArr objectAtIndex:i];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlpath]];

        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"Audio%i.mp3",i]];
        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Successfully downloaded file to %@", path);
            // Init the audio player.


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];

        [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);


        }];



        [operationQueue addOperation:operation];
    }