NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%@----------", [NSThread currentThread]);
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode) {
NSLog(@"返回成功");
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
// 更新头部
[self updateHeaderView:dict];
// 更新未来几天天气
[self updateFutureWeatherView:dict];
});
}
else
NSLog(@"返回失败");
}];
[task resume];
- (void)dataTask
{
// NSURLSessionDataTask:普通GET\POST请求\
// 1. 得到session对象
NSURLSession *session = [NSURLSession sharedSession];
// 2. 创建一个task,任务
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video"];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"-----%lu", data.length);
}];
// 开始任务
[task resume];
}
// 流程:先来一个session,session搞一个代理,搞一个路径,session通过这个路径搞一个下载任务,然后开始任务,任务一旦开启就会调用代理的方法
- (void)downloadTask2
{
NSURLSessionConfiguration *cfg = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:cfg delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos.zip"];
// 2. 创建一个下载task
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url];
NSLog(@"%@",[NSThread currentThread]);
// 如果给下载任务设置了completionHandle这个block,也实现了下载的代理方法,优先执行block
// 3. 开始任务
[task resume];
}
/*
下载完毕调用
@param location 临时文件的路径(下载好的文件)
*/
#pragma mark - NSURLSessionDownloadDelegate
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
// response.suggestedFilename :建议使用的文件名,跟服务器端的文件名一致
NSString *file = [caches stringByAppendingPathComponent:downloadTask.response.suggestedFilename];
// 将临时文件剪切或者复制Caches文件夹
NSFileManager *mgr = [NSFileManager defaultManager];
// AtPath:剪切前的文件路径
// ToPath:剪切后的文件路径
[mgr moveItemAtPath:location.path toPath:file error:nil];
}
/*
恢复下载时调用
@param location 临时文件的路径(下载好的文件)
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
}
/*
每当下载完(写完)一部分时就会调用(可能会被调用多次)
bytesWritten 这次调用写了多少
totalBytesWritten 累计写了多少长度到沙盒中了
totalBytesExpectedToWrite 文件的总长度
*/
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
double progress = (double)totalBytesWritten / totalBytesExpectedToWrite;
NSLog(@"下载进度----%f", progress);
}