AFNetworking 3.0 下载PDF文件

1、创建网络下载对象 

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

2、 设置下载地址 

NSURL *url = [NSURL URLWithString:self.responseModel.pdf_url];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSString *filePath = [NSString setPathOfDocumentsByFileName:[NSString stringWithFormat:@"%@",@"Invoice"]];

NSLog(@"%@",filePath);

3、开始请求下载 

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {

} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

/* 设定下载到的位置 */

NSString *savePath = [filePath stringByAppendingPathComponent:response.suggestedFilename];

NSLog(@"======%@",savePath);

return [NSURL fileURLWithPath:savePath];

} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

  //下载完成之后的操作

[self downloadSuccessWithFilePath:filePath];

}];

[downloadTask resume];

}

你可能感兴趣的:(AFNetworking 3.0 下载PDF文件)