DocumentPickerManager.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // DocumentPickerManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2025/3/21.
  6. //
  7. #import "DocumentPickerManager.h"
  8. #import <UIKit/UIKit.h>
  9. #import <MobileCoreServices/MobileCoreServices.h>
  10. @interface DocumentPickerManager () <UIDocumentPickerDelegate>
  11. {
  12. NSArray *curURLS;
  13. NSInteger PickerTotalNumber;//用户选择的数目
  14. NSInteger didPickerOkNumber;//拷贝到APP内成功的数量
  15. NSInteger didPickerFailNumber;//拷贝到APP内失败的数量
  16. BOOL didNoticeUIType;//本次是否已经通知UI了
  17. }
  18. @property (nonatomic,copy) pickerSuccess didPickSuc;
  19. @end
  20. @implementation DocumentPickerManager
  21. + (instancetype)shareManager {
  22. static DocumentPickerManager *_instance;
  23. static dispatch_once_t onceToken;
  24. dispatch_once(&onceToken, ^{
  25. _instance = [[self alloc] init];
  26. });
  27. return _instance;
  28. }
  29. - (void)openDocumentPickerSuccess:(pickerSuccess)success {
  30. PickerTotalNumber = 0;
  31. didPickerOkNumber = 0;
  32. didPickerFailNumber = 0;
  33. curURLS = @[];
  34. didNoticeUIType = NO;
  35. _didPickSuc = success;
  36. // 创建 UIDocumentPickerViewController
  37. UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeItem] inMode:UIDocumentPickerModeImport];
  38. // 启用多选
  39. documentPicker.allowsMultipleSelection = YES;
  40. // 设置代理
  41. documentPicker.delegate = self;
  42. // 设置模态呈现样式
  43. documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
  44. // 弹出文档选择器
  45. UIViewController* vc = [iTools appRootViewController];
  46. [vc presentViewController:documentPicker animated:YES completion:nil];
  47. }
  48. #pragma mark - UIDocumentPickerDelegate
  49. - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
  50. PickerTotalNumber = urls.count;
  51. curURLS = urls;
  52. for (NSURL *fileURL in urls) {
  53. // 将文件保存到应用的沙盒目录
  54. [self saveFileToAppSandbox:fileURL];
  55. }
  56. }
  57. - (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
  58. // 用户取消了文件选择
  59. HLog(@"Document picker was cancelled");
  60. [self didFinishPickerTaskFun];
  61. }
  62. - (void)saveFileToAppSandbox:(NSURL *)fileURL {
  63. // 获取文件名称
  64. NSString *fileName = [fileURL lastPathComponent];
  65. // 获取应用的 Documents 目录路径
  66. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  67. // NSString *documentsDirectory = [paths firstObject];
  68. NSString *documentsDirectory = kSHPath_FileAPP;
  69. // 构建目标文件路径
  70. NSString *destinationPath = [documentsDirectory stringByAppendingPathComponent:fileName];
  71. NSURL *destinationURL = [NSURL fileURLWithPath:destinationPath];
  72. // 检查文件是否已经存在
  73. if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath]) {
  74. HLog(@"文件已存在: %@", destinationPath);
  75. didPickerOkNumber ++;
  76. [self didFinishPickerTaskFun];
  77. return;
  78. }
  79. // 将文件从临时位置复制到目标路径
  80. KWeakSelf
  81. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  82. NSError *error;
  83. BOOL success = [[NSFileManager defaultManager] copyItemAtURL:fileURL toURL:destinationURL error:&error];
  84. if (success) {
  85. HLog(@"文件保存成功: %@", destinationPath);
  86. self->didPickerOkNumber ++;
  87. } else {
  88. HLog(@"文件保存失败: %@", error.localizedDescription);
  89. self->didPickerFailNumber ++;
  90. }
  91. mainBlock(^{
  92. [weakSelf didFinishPickerTaskFun];
  93. });
  94. });
  95. }
  96. #pragma mark 判断是否处理完成数据
  97. - (void)didFinishPickerTaskFun
  98. {
  99. if (didPickerOkNumber + didPickerFailNumber < PickerTotalNumber ) {
  100. return;
  101. }
  102. HLog(@"处理完成了")
  103. if(!curURLS || didNoticeUIType){
  104. return;
  105. }
  106. if(_didPickSuc){
  107. didNoticeUIType = YES;
  108. _didPickSuc(curURLS);
  109. }
  110. }
  111. @end