使用AFNetworking下载文件

2023-07-11  本文已影响0人  reviewThis

文件存储路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [paths lastObject];

文件下载

NSURLRequest *request = [NSURLRequest requestWithURL:url];
downloadTask = [[AFHTTPSessionManager manager]downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
      NSLog(@"%@", [NSString stringWithFormat:@"当前下载进度:%.2f%%",100.0 * progress.completedUnitCount / progress.totalUnitCount]);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSLog(@"临时文件路径: %@", targetPath);
        NSURL *path = [NSURL fileURLWithPath:destUrl];
        [path URLByAppendingPathComponent:response.suggestedFilename];
        return path;
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        NSLog(@"本地存储路径: %@", filePath);
    }];

Tips

测试链接

1、http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4 1分钟
2、http://vjs.zencdn.net/v/oceans.mp4
3、https://media.w3.org/2010/05/sintel/trailer.mp4 52秒
4、http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4 10分钟

上一篇 下一篇

猜你喜欢

热点阅读