【问题标题】:Unable to select a PowerPoint (PPTX) document with UIDocumentPickerViewController无法使用 UIDocumentPickerViewController 选择 PowerPoint (PPTX) 文档
【发布时间】:2018-12-31 07:01:23
【问题描述】:

只是先清理一些快速项目:

  1. 我不想打开和/或预览文档,因为我看到一个常见的答案是指用户这样做的依赖项
  2. 我不想选择任何其他文件类型。我的应用特别想获取一个 powerpoint 文档,然后将其传递给 API 调用

话虽如此,我无法让我的选择器允许我打开 PPTX,如下所示:

我已采取以下措施来尝试解决此问题:

我对文件运行mdls -name kMDItemContentTypeTree test.pptx 以获取其内容类型。

kMDItemContentTypeTree = (
    "org.openxmlformats.presentationml.presentation",
    "public.data",
    "public.composite-content",
    "public.archive",
    "public.item",
    "org.openxmlformats.presentationml.presentation",
    "org.openxmlformats.openxml",
    "public.zip-archive",
    "public.presentation",
    "public.content",
    "com.pkware.zip-archive" )

然后我将其添加到我的 info.plist 文件中,如下所示:

<dict>
    <key>LSItemContentTypes</key>
    <array>
        <string>org.openxmlformats.presentationml.presentation</string>
        <string>public.data</string>
        <string>public.composite-content</string>
        <string>public.archive</string>
        <string>public.item</string>
        <string>org.openxmlformats.presentationml.presentation</string>
        <string>org.openxmlformats.openxml</string>
        <string>public.zip-archive</string>
        <string>public.presentation</string>
        <string>public.content</string>
        <string>com.pkware.zip-archive</string>
    </array>
    <key>CFBundleTypeName</key>
    <string>PPTX</string>
    <key>LSHandlerRank</key>
    <string>Alternate</string>
</dict>

然后,我引用了这个 Apple site 来获取用于 powerpoint 的 UTI。

com.microsoft.powerpoint.​ppt

最后,我这样设置我的通话:

let powerpointType = "com.microsoft.powerpoint.​ppt"
let documentPicker = UIDocumentPickerViewController(documentTypes: [powerpointType], 
                                                    in: .import)

但是,如初始图像所示,这不允许我选择文件。因此,我希望找到缺失的部分,以便仅选择 powerpoint 文件。

** 更新 ** 根据马特的评论,我确实将值添加到我的 plist 中,但仍然无法选择文件。

我还在 plist 和 UIDocumentPickerViewController 调用中使用 pptx 扩展进行了测试,但也没有成功。

【问题讨论】:

  • 我的猜测是问题在于,虽然 you 知道 "com.microsoft.powerpoint.​ppt" 是扩展名为 PPTX 的文件的 UTI,但运行时并不知道它,并且您没有在 Info.plist 中导出该信息,因此它无法 知道它。
  • 谢谢@matt。我想我之前尝试过一种变体,但我继续测试将该值添加到我的 plist 中,但没有成功。重命名 plist 和 init 调用以使用 pptx 扩展名也不起作用。
  • 不要将 pptx 信息放在“文档类型”下。它需要在“Imported UTIs”下。 “文档类型”适用于您希望自己的应用列在另一个应用的“打开方式”下的情况。
  • 感谢@rmaddy,能够根据您的建议让它发挥作用。

标签: ios swift uti uidocumentpickerviewcontroller


【解决方案1】:

org.openxmlformats.presentationml.presentation 由 iOS 定义,因此您无需将其包含在导入的类型中。该定义已被 iOS 导入。

  • 如果您需要在共享 pptx 文件时让您的应用在共享表中可用,您只需添加一个 CFBundleDocumentTypes 条目。但是既然你说你只能处理这些文件,不能打开它们,这可能不合适。也许你想做的是写一个共享扩展。

  • 如果你只想在UIDocumentPickerViewController 中选择pptx 文件,你不需要在你的plist 文件中添加任何东西!!只需使用 documentTypes:["org.openxmlformats.presentationml.presentation"]

  • 初始化您的选择器

最后是一些cmets:

  • com.microsoft.powerpoint.​ppt 是二进制 PowerPoint 文件 (.ppt) 的 UTI。对于 pptx,您需要 openxmlformats 之一。

  • 对于任何其他类型的选择器,您无需声明支持、定义或添加到您的允许 UTI 列表中。内容类型树 Spotlight 属性中列出的其他类型是 pptx 的父类型。例如,如果您将 public.presentation 添加到您的列表中,您还可以打开 Keynote 文件。这可能不是你想要的。

【讨论】:

  • 谢谢。我只在问题中发布了我们功能的一个子集,因此共享扩展在我们的特定场景中没有好处。只需将“org.openxmlformats.presentationml.presentation”传递给文档选择器就足够了。
  • @Thomas 我正在尝试在文档选择器中打开 com.microsoft.powerpoint.​ppt。但我无法选择 .ppt 文件。请帮帮我
  • @Hitesh 请详细解释发生了什么。
  • @ThomasDeniau 我正在尝试从文档选择器中选择 .ppt 文件,但我不能。在这里,我在文档选择器中传递了以下文件类型。在文档选择器中,我包含了两种类型的文件(.ppt 和 .pptx)["com.microsoft.powerpoint.​ppt","com.microsoft.powerpoint.​pptx","org.openxmlformats.presentationml.presentation" ]
  • @Hitesh 你能修复它吗??
猜你喜欢
  • 2019-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
相关资源
最近更新 更多