使用AFHTTPRequestSerializer 创建NSMutableURLRequest 请求
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:requestUrl parameters:nil error:nil];
设置请求头
[request setAllHTTPHeaderFields:[self requestHeader]];
生成下载任务task
NSURLSessionTask *task = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
/*设置下载路径
[NSURL fileURLWithPath:filePath]
或者
[NSURL URLWithString:[@"file://" stringByAppendingString:filePath]] 都可以,是一样的
*/
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
} else {
}
}] ;
///这里需要手动开启下载任务
[task resume];
注意点
其中的下载路径
[NSURL fileURLWithPath:filePath]
和
[NSURL URLWithString:[@"file://" stringByAppendingString:filePath]]
是相同的