从网址下载AFNetworking

时间:2017-02-11 10:22:09

标签: ios objective-c afnetworking

当用户按下按钮时,我需要从url下载一个文件。如果用户立即按下另一个按钮需要在完成第一个文件后下载该文件。如果按第三个按钮也需要下载该文件。如何实现这一点AFNetworking? 这里我的代码示例下载file.Thanks提前。

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

1 个答案:

答案 0 :(得分: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];
相关问题