我开始使用 NSURLSession 实现下载方法,并成功地从多个请求中下载了不同的文件。但是现在我想添加一个进度轨道,但是没有触发下载进度的代表。
这是我的代码:
NSURLSessionConfiguration *defaultConfigObject = NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue:nil];
NSURLSessionDownloadTask * downloadTask = [defaultSession downloadTaskWithRequest:request completionHandler:^(NSURL * __nullable location,
NSURLResponse * __nullable response, NSError * __nullable error) {
NSData *data = [NSData dataWithContentsOfURL:location];
[[NSFileManager defaultManager] createFileAtPath:docPath contents:data attributes:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:docPath]) {
NSDictionary *notificationDic = [[NSDictionary alloc] initWithObjectsAndKeys:docPath,@"docPath", item, @"item", nil];
[[NSNotificationCenter defaultCenter] postNotificationName: @"openFile" object:nil userInfo:notificationDic];
}
}];
[downloadTask resume];
我的头文件上有 NSURLSessionDownloadDelegate 。
我需要使用完成处理程序才能对文件执行不同的任务。
有什么办法可以做到吗?