+(void) doDownload:(NSString*)localFilePath
{
NSURLSessionConfiguration *configuration =[NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager =[[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *servicePath =[NSString stringWithFormat:DOWNLOAD_RESUME_FILE_URL, [enterpriseId stringByAppendingPathExtension:@"zip"]];
NSURL *URL =[NSURL URLWithString:servicePath];
NSURLRequest *request =[NSURLRequest requestWithURL:URL];
// targetPath是下载的临时文件路径,:app_dir/tmp/CFNetworkDownload_9z499O.tmp
NSURL*(^destinationBlock) (NSURL *targetPath, NSURLResponse *response) = ^NSURL* (NSURL *targetPath, NSURLResponse *response){
return[NSURL fileURLWithPath:localFilePath];// 下载文件最终存放地址,不同于targetPath
};
// filePath即上面那个block的返回值
void(^completionBlock) (NSURLResponse *response, NSURL *filePath, NSError *error) = ^void (NSURLResponse *response, NSURL *filePath, NSError *error){
NSFileManager *fileManager =[NSFileManager defaultManager];
NSString *zipFilePath =[filePath path];// 将NSURL转成NSString
if([fileManager fileExistsAtPath:zipFilePath]){
[fileManager removeItemAtPath:zipFilePath error:nil];
} };
NSURLSessionDownloadTask *downloadTask =[manager downloadTaskWithRequest:request progress:nil destination:destinationBlock completionHandler:completionBlock];
[downloadTask resume];
}
网友评论