+ (void)downloadVideoWithUrl:(NSString *)videoUrl
progress:(void (^)(NSProgress *downloadProgress)) downloadProgressBlock
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler
{
NSParameterAssert(videoUrl);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURL *url = [NSURL URLWithString:videoUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [self videoUrlWithPath:response.suggestedFilename];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (completionHandler) {
completionHandler(response,filePath,error);
}
}];
[download resume];
}
+ (NSURL *)videoUrlWithPath:(NSString *)component
{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:component];
return [NSURL fileURLWithPath:path];
}
网友评论