【问题标题】:Setting document extensions in UIDocumentPicker in swift快速在 UIDocumentPicker 中设置文档扩展名
【发布时间】:2018-08-29 06:34:45
【问题描述】:

我正在使用文档选择器来选择文档并上传到服务器。我只需要为允许的文档传递一些扩展名,但是我无法找出扩展名代表什么类型的文档。这是我想要允许的扩展列表。 - docx - Microsoft Word - xlsx - 微软 Excel - pptx - Microsoft Powerpoint - pdf - CSV - 页面 - 数字 - 关键 - 主题演讲 - rtf - 文本文件 我知道一些扩展名,如“kUTTypePDF,kUTTypeRTF”,但不是所有的都请帮忙。

【问题讨论】:

    标签: ios swift uikit


    【解决方案1】:

    Swift 5.0

    let types = [kUTTypePDF, kUTTypeText, kUTTypeRTF, kUTTypeSpreadsheet] // You can add more types here as pr your expectation
    let importMenu = UIDocumentPickerViewController(documentTypes: types as [String], in: .import)
    

    试试这个

    如果要选择所有文件,则必须使用以下代码:

    let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item", "public.content", "public.audiovisual-content", "public.movie", "public.audiovisual-content", "public.video", "public.audio", "public.text", "public.data", "public.zip-archive", "com.pkware.zip-archive", "public.composite-content"], in: .import)
    

    【讨论】:

    • 谢谢@Nikunj,但我只想要选定的文件(- docx - Microsoft Word - xlsx - Microsoft Excel - pptx - Microsoft Powerpoint - pdf - csv - pages - numbers - key - Keynote - rtf - txt )。
    【解决方案2】:

    Apple 在此处发布了内置文档类型的列表:

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

    我相信应该有你要找的所有东西。如果没有,那么您将不得不添加自己的类型。您可以通过在 Xcode 中选择 Project 并进入 Info 选项卡来执行此操作。然后在底部,为每种文件类型添加一个新的 Imported UTI。

    • 描述:用户友好的描述
    • 标识符:这是您将在 UIDocumentPickerViewController 的 documentTypes 参数中使用的标识符。 com.yourcompany.yourproduct.fileextension 或类似名称。
    • 符合:“public.data, public.content”

    然后在 Additional Imported UTI 属性下单击以添加以下内容:

    UTTypeTagSpecification (Dictionary)
        public.filename-extension (Array)
            Item 0 (String): the file extension without "." (i.e. "csv")
    

    【讨论】:

    • 在 Xcode 12 中,该部分称为“Imported Type Identifiers”
    【解决方案3】:

    对于 .doc.docx,您可以使用 "com.microsoft.word.doc""org。 openxmlformats.wordprocessingml.document"

    对于.xlsx,可以使用"org.openxmlformats.spreadsheetml.sheet"

    对于 .xls,您可以使用 "com.microsoft.excel.xls"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多