// // DocumentPickerManager.m // 隐私保护 // // Created by xd h on 2025/3/21. // #import "DocumentPickerManager.h" #import #import @interface DocumentPickerManager () { NSArray *curURLS; NSInteger PickerTotalNumber;//用户选择的数目 NSInteger didPickerOkNumber;//拷贝到APP内成功的数量 NSInteger didPickerFailNumber;//拷贝到APP内失败的数量 } @property (nonatomic,copy) pickerSuccess didPickSuc; @end @implementation DocumentPickerManager + (instancetype)shareManager { static DocumentPickerManager *_instance; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; } - (void)openDocumentPickerSuccess:(pickerSuccess)success { PickerTotalNumber = 0; didPickerOkNumber = 0; didPickerFailNumber = 0; curURLS = @[]; _didPickSuc = success; // 创建 UIDocumentPickerViewController UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeItem] inMode:UIDocumentPickerModeImport]; // 启用多选 documentPicker.allowsMultipleSelection = YES; // 设置代理 documentPicker.delegate = self; // 设置模态呈现样式 documentPicker.modalPresentationStyle = UIModalPresentationFormSheet; // 弹出文档选择器 UIViewController* vc = [iTools appRootViewController]; [vc presentViewController:documentPicker animated:YES completion:nil]; } #pragma mark - UIDocumentPickerDelegate - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls { PickerTotalNumber = urls.count; curURLS = urls; for (NSURL *fileURL in urls) { // 将文件保存到应用的沙盒目录 [self saveFileToAppSandbox:fileURL]; } } - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller { // 用户取消了文件选择 HLog(@"Document picker was cancelled"); [self didFinishPickerTaskFun]; } - (void)saveFileToAppSandbox:(NSURL *)fileURL { // 获取文件名称 NSString *fileName = [fileURL lastPathComponent]; // 获取应用的 Documents 目录路径 // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documentsDirectory = [paths firstObject]; NSString *documentsDirectory = kSHPath_FileAPP; // 构建目标文件路径 NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName]; NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath]; // 检查文件是否已经存在 if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) { HLog(@"文件已存在: %@", destinationPath); didPickerOkNumber ++; [self didFinishPickerTaskFun]; return; } // 将文件从临时位置复制到目标路径 KWeakSelf dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSError *error; BOOL success = [[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:destinationURL error:&error]; if (success) { HLog(@"文件保存成功: %@", destinationPath); self->didPickerOkNumber ++; } else { HLog(@"文件保存失败: %@", error.localizedDescription); self->didPickerFailNumber ++; } mainBlock(^{ [weakSelf didFinishPickerTaskFun]; }); }); } #pragma mark 判断是否处理完成数据 - (void)didFinishPickerTaskFun { if (didPickerOkNumber + didPickerFailNumber < PickerTotalNumber ) { return; } HLog(@"处理完成了") if(!curURLS || curURLS.count == 0){ return; } if(_didPickSuc){ _didPickSuc(curURLS); } } @end