Files
mier_ios/SweetParty/主类/RCMic/Room/GiftView/QGVAPWrapView+download.m
2025-08-11 10:43:19 +08:00

55 lines
2.3 KiB
Objective-C
Executable File

//
// QGVAPWrapView+download.m
// huixiang
//
// Created by bj_szd on 2023/2/22.
// Copyright © 2023 Echo. All rights reserved.
//
#import "QGVAPWrapView+download.h"
@implementation QGVAPWrapView (download)
- (void)playMemoryWithStr:(NSString *)urlStr Success:(void(^)(NSURL *filePath))success Failure:(void(^)(NSError *error))failure {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *fileName = [urlStr lastPathComponent]; //获取文件名称
NSURL *URL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//判断是否存在
if([self isFileExist:fileName]) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *filePath = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
if (success) {
success(filePath);
}
}else {
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress *downloadProgress){
} destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
NSURL *url = [documentsDirectoryURL URLByAppendingPathComponent:fileName];
return url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (success) {
success(filePath);
}
}];
[downloadTask resume];
}
}
//判断文件是否已经在沙盒中存在
-(BOOL)isFileExist:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL result = [fileManager fileExistsAtPath:filePath];
return result;
}
@end