2016-06-21 36 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