接上一篇文章,增加下载方法,如需要进度,在这里 progress:nil 传入 NSProgress 对象的地址就行了
- (void)downloadTaskWithURL:(NSURL *)URL destination:(void(^)(NSURL *targetPath, NSURLResponse *response))destination completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath,NSError *error))completionHandler
{
NSURLRequest *request = [NSURLRequestrequestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [_sessionManagerdownloadTaskWithRequest:requestprogress:nildestination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManagerdefaultManager] URLForDirectory:NSDocumentDirectoryinDomain:NSUserDomainMaskappropriateForURL:nilcreate:NOerror:nil];
destination(targetPath, response);
return [documentsDirectoryURLURLByAppendingPathComponent:[responsesuggestedFilename]];
} completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {
completionHandler(response, filePath, error);
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
}