美文网首页
让App支持系统/自定义类型文件打开(自定义类型:ofd)

让App支持系统/自定义类型文件打开(自定义类型:ofd)

作者: __Gavin__ | 来源:发表于2021-05-26 15:15 被阅读0次

    一、支持打开

    1、系统文件类型

    苹果系统类型的官方文档:地址

    以 PDF 为例,在 info.plist 里配置 Document types 如下:


    image.png
    • Document Type Name:文档的类型名称(PDF)。
    • Handler rank:包含Owner、Alternate 、Default、None四个可选值,指定对于某种类型的优先权级别,而Launcher Service会根据这个优先级别来排列显示的App的顺序。优先级别从高到低依次是Owner、Alternate、Default、None表示不接受这种类型。
    • Document Content Type UTIs - Item0: 文件类型的 identifier,系统文件可以从上面官方文档查询。

    2、自定义类型

    添加自定义类型

    以 ofd 文件为例,在 info.plist 里配置 Exported Type Identifiers 如下:


    Xcode11 Xcode12
    • Identifier:唯一ID,需要保持唯一性。
    • public.filename-extension(Extensions):文件类型。
    配置可打开类型

    在 info.plist 里配置 Document types 如下:


    image.png

    二、支持导入

    1、系统文件类型

    以 PDF 为例,使用 UIDocumentPickerViewController 读取“文件”App内的 PDF 文档,代码如下:

    let documentTypes = ["com.adobe.pdf"]
    let documentVC = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
    documentVC.delegate = self
    documentVC.transitioningDelegate = self
    documentVC.modalPresentationStyle = .fullScreen
    self.present(documentVC, animated: true, completion: nil)
    

    2、自定义类型

    添加自定义类型

    以 ofd 文件为例,在 info.plist 里配置 Imported Type Identifiers 如下:


    Xcode11 Xcode12
    读取文件

    使用 UIDocumentPickerViewController 读取“文件”App内的 ofd 文档,代码如下:

    let documentTypes = ["com.adobe.pdf", "com.inbasis.ofd"]
    let documentVC = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
    documentVC.delegate = self
    documentVC.transitioningDelegate = self
    documentVC.modalPresentationStyle = .fullScreen
    self.present(documentVC, animated: true, completion: nil)
    

    三、总结

    1. 系统文件读取
      • 无需配置,直接UIDocumentPickerViewController代码读取
    2. 非系统文件读取
      • 配置 Imported Type UTIs
      • UIDocumentPickerViewController代码读取
    3. 支持系统文件打开
      • 配置 Document types
    4. 支持非系统文件打开
      • 配置 Exported Type UTIs
      • 配置 Document types

    相关文章

      网友评论

          本文标题:让App支持系统/自定义类型文件打开(自定义类型:ofd)

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