2017-02-11 101 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?在第一次按钮点击后,如果用户点击第二个,我应该下载这两个文件,但有各自的按钮点击顺序。谢谢。 –