//
// ViewController.m
// NSURLSessionDownTask
//
// Created by hq on 16/4/17.
// Copyright © 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURLSession *session=[NSURLSession sharedSession];
NSString *urlString=@"http://xxxx/resources/videos/xxxx.mp4";
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSURLSessionDownloadTask *task=[session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//该方法会将文件默认下载到我们的tmp文件夹中,我们还需要将该文件移动到我们的cache当中
NSString *destPath=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *fileName=[destPath stringByAppendingPathComponent:response.suggestedFilename];
NSFileManager *fileManager=[NSFileManager defaultManager];
//将文件移动到我们的cache文件夹当中
[fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:fileName] error:nil];
NSLog(@"%@",fileName);
}];
[task resume];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
NSURLSessionDownloadTask下载内容
最新推荐文章于 2020-05-10 09:44:04 发布