关于Objective C:为我的应用程序的文档类型注册图标

Registering an icon for my application's document type

我正在尝试为我的应用程序的文档类型注册一个图标。在阅读了声明新的统一类型标识符并查看/Developer/Examples/Sketch之后,我在我的Info.plist

中提出了类似的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<key>CFBundleDocumentTypes</key>

  <dict>
    <key>CFBundleTypeRole</key>
    <string>Viewer</string>
    <key>LSItemContentTypes</key>
   
      <string>com.mycompany.myextension</string>
    </array>
    <key>NSDocumentClass</key>
    <string>NSString</string>
  </dict>
</array>

...

<key>UTExportedTypeDeclarations</key>

  <dict>
    <key>UTTypeDescription</key>
    <string>Blah blah blah</string>
    <key>UTTypeConformsTo</key>
   
      <string>public.data</string>
    </array>
    <key>UTTypeIconFile</key>
    <string>My-file-icon.icns</string>
    <key>UTTypeIdentifier</key>
    <string>com.mycompany.myextension</string>
    <key>UTTypeTagSpecification</key>
    <dict>
      <key>public.filename-extension</key>
     
        <string>myextension</string>
      </array>
    </dict>
  </dict>
</array>

现在,一切都很好,很花哨,即,当我单击带有扩展名的文件时打开了我的程序,等等。但是,文档图标未在操作系统中注册,即,我看到的是一个丑陋的空白图标,而不是我的漂亮的My-file-icon.icns。我怀疑我在上面的plist中缺少任何想法吗?


尝试将图标名称放在CFBundleDocumentTypes数组的CFBundleTypeIconFile键中,而不是UTExportedTypeDeclarations数组中。

当然,请确保" My-file-icon.icns "位于目标的"副本捆绑资源"构建阶段,并且已复制到应用程序捆绑中的"内容/资源"中。 >


我一直试图在Xcode 11中做到这一点,这一直是一个挑战。这样的问题在这里有很多答案,但是我找不到能够解决此问题的所有东西。我会在这里尝试做。

技术问答


我也遇到了类似的问题,事实证明,我需要根据我在此处记录的内容来重建启动服务数据库。...

https://stackoverflow.com/a/12466968/1571635


您在Info.plist中的UTI声明似乎是正确的,但是我注意到了另一个问题。如果您的应用程序是基于文档的应用程序,则需要使用NSDocument子类替换以下条目中的NSString:

1
2
<key>NSDocumentClass</key>
<string>NSString</string>

例如,在Sketch中为" SKTDocument ":

1
2
<key>NSDocumentClass</key>
<string>SKTDocument</string>

编辑:
还请确保为导出的UTI使用您自己的反向域名。这样可以确保UTI是唯一的。例如,在我的情况下为com.mindnode.MindNode.MindNodeDocument。