UIDocumentPickerViewController和UIDocumentInteractionController

UIDocumentPickerViewController和UIDocumentInteractionController

UIDocumentPickerViewController

重点要了解UIDocumentPickerViewController在初始化的时候inMode的四种模式:

  1. UIDocumentPickerModeImport: Import从提供者那里获得文件并拷贝到我们的host app。最经典的应用场景是在内容创建类应用中的使用。例如,像keynote、PowerPoint这样的演示制作应用,希望导入图片,视频,以及音频。它们希望拷贝一份这些数据以此保证它们随时可用。
  2. UIDocumentPickerModeOpen: 和import一样,open同样从文件提供者那里获得数据并导入我们的host app,只是不同的是,这些数据没有被拷贝一份至我们的host app,数据还在原处。例如,你或许在音乐播放器、视频播放器,再或者图像编辑器中使用该方式。
  3. UIDocumentPickerModeExportToService: Export使我们的host app可以保存文件至其它提供者。例如,这些提供者可能是常用的像Dropbox、iCloud Drive这样的云存储系统。host app可以通过export保存文件到提供者的存储空间。在接下来的编辑器例子中,当用户完成编辑,他们可以导出文件,然后稍后可以在其它app中打开这些文件。
  4. UIDocumentPickerModeMoveToService: 除了host app不会持有一份儿文件的拷贝,其它Moving和export差不多。这或许是最不常用的操作,因为大多数iOS apps不是为了抛弃它们的数据才创建的。

UIDocumentInteractionController

这个类相对简单一点,重点是要实现在delegate里面的3个方法, 这样视图控制器才能显示出来:

#pragma mark - UIDocumentInteractionControllerDelegate
-(UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
    return self;
}
-(UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
    return self.view;
}
-(CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
    return self.view.bounds;
}

UTI

苹果支持的文件类型使用UTI标识

Apple 文档:https://developer.apple.com/library/archive/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

在info.plist上配置app里面支持读取查看支持的文件类型

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeIconFiles</key>
            <array/>
            <key>CFBundleTypeName</key>
            <string>com.myApp.surpportFiles</string>
            <key>LSItemContentTypes</key>
            <array>
                <string>public.image</string>
                <string>public.audio</string>
                <string>public.movie</string>
                <string>public.data</string>
                <string>public.text</string>
                <string>public.archive</string>
                <string>public.item</string>
                <string>public.source-code</string>
            </array>
        </dict>
    </array>

猜你喜欢

转载自www.cnblogs.com/zhouhui231/p/12183125.html