如何将 UTType 与 UIdocumentPicker 一起使用(页面?docx?)

Jul*_*ang 2 types file swift uidocumentpickerviewcontroller uttype

我正在尝试打开一个仅允许某些文件类型的选择器。我正在使用此文档来查找我需要的 UTTYpe: https://developer.apple.com/documentation/uniformtypeidentifiers/uttype/system_declared_uniform_type_identifiers

但我找不到 .pages、.doc、.docx 的 UTTYpe...

我应该怎么办?

这是我目前的代码

    func presentPickerDocument() {
    //Create a picker specifying file type and mode
    var supportedTypes: [UTType]
    supportedTypes = [UTType.pdf, UTType.url]

    let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
    documentPicker.delegate = self
    documentPicker.allowsMultipleSelection = false
    present(documentPicker, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

Swe*_*per 9

UTType您可以从文件扩展名创建一个,如下所示:

UTType(tag: "pages", tagClass: .filenameExtension, conformingTo: nil)
Run Code Online (Sandbox Code Playgroud)

这会创建一个UTType?,因为不能UTType与给定的文件扩展名关联。

您还可以使用:

UTType(filenameExtension: "pages")
Run Code Online (Sandbox Code Playgroud)

但这仅UTType在文件扩展名继承自 时找到 a public.data,因此它不适用于所有文件扩展名。

在 macOS 终端上,您还可以使用该mdls命令检查文件的统一类型标识符

mdls -name kMDItemContentType yourFile.pages
Run Code Online (Sandbox Code Playgroud)

或者它的整个树:

mdls -name kMDItemContentTypeTree yourFile.pages
Run Code Online (Sandbox Code Playgroud)

之后,您可以UTType使用其标识符创建一个,如下所示:

UTType("com.apple.iwork.pages.sffpages")
Run Code Online (Sandbox Code Playgroud)