2016-06-21 54 views
0

我有一个array,其中包含不同的网址。我想用进度条下载一个文件,而不是启动下一个文件等等。使用网络交友一次下载一个文件

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

-(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?

回答

2

在下面的函数中声明一个全局文件NSMutableArray并使用它。

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

现在调用该函数而是initialize self.fileArr之前和呼叫downloadFile方法之后。

希望这会帮助你。

+0

即使在文件下载失败的错误下,您应该继续处理下一个下载并处理失败的情况。 – 2016-06-21 10:21:28

+0

现在可以......... :) –

+0

如何计算进度。 @NiravDoctorwala – salmancs43

1

给予限制队列一次操作的时间,

对于这一点,尽量给你排队之前每个操作之间添加依赖。

如果在添加队列之前添加两个操作之间的依赖关系,比如operation1和operation2,则操作2将不会启动,直到操作1完成或取消为止。

不喜欢它:

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

// Make operation2 depend on operation1 

[operation2 addDependency:operation1]; 

[operationQueue addOperations:@[operation1, operation2, operation3] waitUntilFinished:NO]; 

UPDATE

// Create a http operation 

NSURL *url = [NSURL URLWithString:@"http://yoururl"]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 

[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    // Print the response body in text 

    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]); 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    NSLog(@"Error: %@", error); 

}]; 

// Add the operation to a queue 
// It will start once added 

NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; 
[operationQueue addOperation:operation]; 

希望它能帮助你..!

+0

如果你有一些教程,请参考。我正在使用AFNetworking第一次 – salmancs43

相关问题