文件下载(AFN3.0+)设置请求头
//AFN3.0+基于封住URLSession的句柄
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlString parameters:nil error:nil];
// 设置请求头
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
[requestsetValue:__Encrypt(currentVersion,2)forHTTPHeaderField:@"fdata"];// 当前版本
[requestsetValue:@"ios" forHTTPHeaderField:@"androidorios"];// 区分系统
[requestsetValue:__Encrypt([[NSUserDefaults standardUserDefaults]objectForKey:@"number"],2)forHTTPHeaderField:@"fnumber"];// 工号
request.timeoutInterval=60.f;
//请求
// NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//下载Task操作
NSURLSessionDownloadTask*downloadTask = [managerdownloadTaskWithRequest:requestprogress:^(NSProgress*_NonnulldownloadProgress) {// 回到主队列刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
progressHandle(downloadProgress);
});
}destination:^NSURL*_Nonnull(NSURL*_NonnulltargetPath,NSURLResponse*_Nonnullresponse) {
//- block的返回值, 要求返回一个URL, 返回的这个URL就是文件的位置的路径
// 每个功能调用时单独对路径进行设置,处理完成后返回下载的路径
NSString*path = destinationHandle(response.suggestedFilename);
NSURL*url = [NSURL fileURLWithPath:path];
returnurl; }completionHandler:^(NSURLResponse*_Nonnullresponse,NSURL*_NullablefilePath,NSError*_Nullableerror) {
// filePath就是你下载文件的位置,你可以解压,也可以直接拿来使用
completionHandle(filePath,error);
}];
//开始下载
[downloadTaskresume];