2017-02-11 99 views
0

當用戶按下按鈕時,我需要從url下載一個文件。如果用戶立即按下另一個按鈕,需要在第一個文件完成後下載該文件。立即如果按下第三個按鈕也需要下載該文件。通過AFNetworking實現這一目標? 在這裏我的代碼示例下載file.Thanks提前。AFNetworking從網址下載

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

manager.responseSerializer = [AFCompoundResponseSerializer serializer]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",@"audio/mp4",nil]; 

AFHTTPRequestOperation *operation = [manager GET:[array objectAtIndex:0] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    if (responseObject) { 

     NSData *data=[[NSData alloc] initWithData:responseObject]; 

     NSLog(@"Download Succesfully Completed"); 

     //after completion I'm implementing my method here 

     [self sentMsgSaveWithData:data orUrl:@"" withBool:YES withMsg_ID:@"" withDict:tempDict]; 


    } else { 
     NSLog(@"Download Error"); 

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


}]; 

[operation start]; 
+0

你必須管理的下載請求時,用戶按下按鈕。而在後臺,你必須一步 –

+0

下載步驟中,您可以使用同步它。搜索它在ios同步donwload –

+0

@ Himanshu Moradiya:如何處理請求? –

回答

0

嘗試用默認的配置會話:

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

    NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { 

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; 
     return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; 
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { 
     NSLog(@"File downloaded to: %@", filePath); 
    }]; 
    [downloadTask resume]; 
+0

我只想要文件數據。如何獲取文件數據? –

+0

你的文件是從那裏filePath downladed你可以做任何你想要你的文件 – Aragunz

+0

但我想一個一個地下載各自的按鈕press.So我需要去與NSOperationQueue?在第一次按鈕點擊後,如果用戶點擊第二個,我應該下載這兩個文件,但有各自的按鈕點擊順序。謝謝。 –