美文网首页iOS技术交流收藏iOS
访问沙盒外文档UIDocumentPickerViewContr

访问沙盒外文档UIDocumentPickerViewContr

作者: 莫云溪 | 来源:发表于2018-06-05 09:25 被阅读0次

官方文档解析

官方文档:https://developer.apple.com/documentation/uikit/uidocumentpickerviewcontroller?language=objc

UIDocumentPickerViewController有四种模式:

  • Import an external document:用户选择一个外部文档,文档选择器拷贝该文档到应用沙盒,不会修改源文档。
  • Export a local document:文档选择器拷贝文档到一个外部路径,不会修改源文档。
  • Open an external document:打开一个外部文档,用户可以修改该文档。
  • Move a local document:拷贝文档到外部路径,同时可以修改该拷贝。

操作外部文件注意事项

  • open与move操作会提供外部文件的security-scoped URL 。调用startAccessingSecurityScopedResource开始访问,访问完成调用stopAccessingSecurityScopedResource
  • 使用NSFileCoordinator来操作外部文件
  • 使用NSFilePresenter来展示外部文件内容
  • 不要存储security-scoped URL

调用Demo

展示文件选择

- (void)presentDocumentPicker {
    NSArray *types = @[]; // 可以选择的文件类型
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeOpen];
    documentPicker.delegate = self;
    documentPicker.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

用户选择文件回调

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
    BOOL canAccessingResource = [url startAccessingSecurityScopedResource];
    if(canAccessingResource) {
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        [fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
            NSData *fileData = [NSData dataWithContentsOfURL:newURL];
            NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath = [arr lastObject];
            NSString *desFileName = [documentPath stringByAppendingPathComponent:@"myFile"];
            [fileData writeToFile:desFileName atomically:YES];
            [self dismissViewControllerAnimated:YES completion:NULL];
        }];
        if (error) {
            // error handing
        }
    } else {
        // startAccessingSecurityScopedResource fail
    }
    [url stopAccessingSecurityScopedResource];
}

文件类型定义

initWithDocumentTypes:inMode:中,types需要传入一个uniform type identifiers (UTIs)数组。关于UTIs的官方文档,常见列表


欢迎关注我的博客

相关文章

  • 访问沙盒外文档UIDocumentPickerViewContr

    官方文档解析 官方文档:https://developer.apple.com/documentation/uik...

  • 沙盒

    1、沙盒 程序只能访问自己的沙盒 NSHomeDirectory() 访问沙盒路径 沙盒下有三个目录:Docume...

  • 05-iOS数据存储

    一、iOS沙盒机制 iOS的每个应用都有属于自己的存储空间,即沙盒应用只能访问自己的沙盒,不可访问其他区域。 沙盒...

  • 沙盒

    沙盒(英语:sandbox,又译为沙箱),iOS下的沙盒机制规定app只能访问沙盒目录下的内容(除了用户授权访问通...

  • iOS 沙盒

    沙盒机制:在iOS中每个APP都拥有自己的沙盒,APP只能访问对应沙盒中存储的数据, iOS是不允许跨越沙盒去访问...

  • iOS开发之沙盒机制

    沙盒 iOS系统,每个应用都有自己的沙盒,每个沙盒都是相互独立的,不能互相访问。 获取沙盒路径的代码: NSHom...

  • 数据持久化存储

    沙盒 iOS程序默认情况下只能访问自己的目录,这个目录被称作沙盒 沙盒结构 沙盒结构主要为 DocumentLib...

  • iOS的永久话储存

    沙盒:IOS应用中每个应用均有自己沙盒,用来储存APP自己的数据,每个应用的沙盒均是应用特有的不能交叉访问。 沙盒...

  • iOS面试题

    简述沙盒机制是什么 1.每一个应用程序都有一个属于自己的沙盒目录 2.只能访问自己的沙盒目录 3.不能访问其他应用...

  • 访问手机沙盒

    Xcode 处于调试环境时 1、工具栏 Window -----> Devices and Simulators ...

网友评论

    本文标题:访问沙盒外文档UIDocumentPickerViewContr

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