美文网首页
UIDocumentPickerViewControlle i

UIDocumentPickerViewControlle i

作者: 幻影道哥 | 来源:发表于2022-11-18 17:21 被阅读0次

1.模态打开系统自带的系统文件 选取文件
UIDocumentPickerViewController *documentPickerVC = [[UIDocumentPickerViewController alloc] initWithURL:[NSURL fileURLWithPath:fullPath] inMode:UIDocumentPickerModeExportToService];
// 设置代理
documentPickerVC.delegate = self;
// 设置模态弹出方式
documentPickerVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self.navigationController presentViewController:documentPickerVC animated:YES completion:nil];

2.获取沙盒路径
// 获得文件沙盒地址

  • (NSString *)getNativeFilePath:(NSString *)fileName {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *munu = [NSString stringWithFormat:@"%@/%@",@"downLoad",fileName];
    NSString *filePath = [path stringByAppendingPathComponent:munu];
    // 判断是否存在,不存在则创建
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // fileExistsAtPath 判断一个文件或目录是否有效,isDirectory判断是否一个目录
    BOOL isDir = NO;
    NSMutableArray *theArr = [[filePath componentsSeparatedByString:@"/"] mutableCopy];
    [theArr removeLastObject];
    NSString *thePath = [theArr componentsJoinedByString:@"/"];
    BOOL existed = [fileManager fileExistsAtPath:thePath isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) ) { // 如果文件夹不存在
    [fileManager createDirectoryAtPath:thePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return filePath;
    }

3.文件写入制定的路径
BOOL downResult = [fileData writeToFile:fullPath atomically:YES];

  1. UIDocumentPickerDelegate的代理方法

pragma mark - UIDocumentPickerDelegate

  • (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    // 获取授权
    BOOL fileUrlAuthozied = [urls.firstObject startAccessingSecurityScopedResource];
    if (fileUrlAuthozied) {
    // 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
    NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
    NSError *error;

      [fileCoordinator coordinateReadingItemAtURL:urls.firstObject options:0 error:&error byAccessor:^(NSURL *newURL) {
          // 读取文件
          NSString *fileName = [newURL lastPathComponent];
          NSError *error = nil;
          NSData *fileData = [NSData dataWithContentsOfURL:newURL options:NSDataReadingMappedIfSafe error:&error];
          if (error) {
              // 读取出错
          } else {
             
           
             
              // [self uploadingWithFileData:fileData fileName:fileName fileURL:newURL];
          }
    

// [self dismissViewControllerAnimated:YES completion:NULL];
}];
[urls.firstObject stopAccessingSecurityScopedResource];
} else {
// 授权失败
}
}

相关文章

  • UIDocumentPickerViewControlle i

    1.模态打开系统自带的系统文件 选取文件UIDocumentPickerViewController *docum...

  • āiáiǎiài

    买了新的唇膏 水蜜桃配仙草 楼上的邻居 闹钟响第四遍的时候 发觉是爱情的马脚 偷笑着我没睡着 什么过不去还舍不得忘...

  • I f I f I f I f I f I f I f I f

    文/雅雅 以思考,进化时代。 2018年的第一篇文章,我一直思考书写的使命是什么? 对于我,对于我以外的看他的你。...

  • 【I原创I】I花园

    早些年的奇闻异事 偏偏不生杂草 把春天还给篝火 你祛毒 你神性 你感动于 光合作用的前所未有

  • 面试细节: i = i++和 i = ++i

    作者:默辩 来源:https://blog.csdn.net/qq_44377709/article/detail...

  • i++ ++i

    区分变量的值与表达式的值 变量名:也称标识符,一个 JavaScript 标识符必须以字母、下划线(_)或者美元符...

  • i++ ++i

    区分变量的值与表达式的值 i++ :表达式取原值,j但i的值是加1或减1之后的值++i :表达式和i的值都是加1或...

  • i++ ++i

    区分变量的值与表达式的值 i++ :表达式取原值,j但i的值是加1或减1之后的值++i :表达式和i的值都是加1或...

  • I am I

    老子在《道德经》中说:“知人者智,自知者明。许多人没活明白,大致原因是不知道自己是什么样的人,喜欢什么,讨厌什么。...

  • I AM I

    今天在阅读简书文章的时候,看到下面的一段话。 突然间想到好朋友曾经也对我说过我有点好高骛远,不够踏实,再一次的意识...

网友评论

      本文标题:UIDocumentPickerViewControlle i

      本文链接:https://www.haomeiwen.com/subject/sdmpxdtx.html