【问题标题】:How to define a Uniform Type Identifier in a plist file?如何在 plist 文件中定义统一类型标识符?
【发布时间】:2011-09-05 15:55:32
【问题描述】:

我的应用程序使用以下NSApplicationDelegate 函数。

- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames;

我想使用此功能使用户能够将图像文件拖放到 Dock 中的应用程序图标上。

如何在我的plist 文件中定义某些文件类型以将它们限制为图像?我发现结构必须看起来像这样。

// plist file contents taken from Preview.app
[...]
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeIconFile</key>
        <string>jpeg.icns</string>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>LSIsAppleDefaultForType</key>
        <true/>
        <key>LSItemContentTypes</key>
        <array>
            <string>public.jpeg</string>
        </array>
        <key>NSDocumentClass</key>
        <string>PVDocument</string>
    </dict>
</array>

我将它添加到plist 文件中,但它不起作用。弹出窗口显示以下错误消息。

无法打开文档“test.jpg”。 MyApp 无法打开文件 “JPEG 图像”格式。

此外,我读到 in the documentationpublic.image 这将是我想要定义的。

同时,我发现plist 文件仅包含密钥CFBundleDocumentTypes,如果我使用“创建基于文档的应用程序”选项创建一个 Cocoa 应用程序。您能否澄清一下该选项存在哪些依赖项?

【问题讨论】:

    标签: objective-c drag-and-drop mime-types uti


    【解决方案1】:
        <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>png</string>
                    <string>jpg</string>
                                ... add as many types as you need
                </array>
                        ... other keys
            </dict>
        </array>
    

    更新:CFBundleDocumentTypes 键在 Mac OS X v10.5 中已弃用。应该改用新的键 LSItemContentTypes。这些项目是 UTI 字符串:

    <key>LSItemContentTypes</key>
    <array>
        <string>public.png</string>
    </array>
    

    【讨论】:

    • 两个问题:(1)类型是否区分大小写;换句话说:我是否还需要添加“PNG”和“JPG”以及混合变体? (2) 你知道他们这里为什么不使用UTI方案(反向DNS)吗?
    • 抱歉,信息已过时。 CFBundleDocumentTypes 仍然有效,但已被弃用。 (1) 不区分大小写 (2) 查看更新。
    • 同样的错误信息。 dict 元素仅包含 CFBundleTypeRoleCFBundleTypeExtensions 并具有建议的值 - 但它仍然不起作用。
    • 同时,我创建了一个新的干净项目并按照您的建议编辑了 plist。有用。所以问题一定出在其他地方。-(
    • 不同之处可能在于 MyApp 是否是基于文档的应用程序,但新的 clean 项目不是。我看到您在 plist 中使用 PVDocument 类。在这种情况下,您不需要在 PVDocument 子类中实现 [NSApplicationDelegate application: openFiles:],而是 [NSDocument readFromFileWrapper: ofType: error:]。
    【解决方案2】:

    如果你的文档类型是普通类型,你可以使用UTI's About UTI's

    【讨论】:

    • 对不起,这是一个死链接
    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2018-08-21
    • 2012-09-26
    • 2011-04-23
    相关资源
    最近更新 更多