简体   繁体   中英

Registering an icon for my application's document type

I'm trying to register an icon for my app's document type. After reading Declaring New Uniform Type Identifiers and looking at /Developer/Examples/Sketch I came up with something like this in my Info.plist :

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

...

<key>UTExportedTypeDeclarations</key>
<array>
  <dict>
    <key>UTTypeDescription</key>
    <string>Blah blah blah</string>
    <key>UTTypeConformsTo</key>
    <array>
      <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>
      <array>
        <string>myextension</string>
      </array>
    </dict>
  </dict>
</array>

Now, everything is fine and dandy, ie my program is opened when I click on a file with my extension, etc. However, the document icon is not registered with the OS, ie I see an ugly blank icon instead of my beautiful My-file-icon.icns . I suspect that I'm missing something in the plist above, any ideas?

Try putting the icon name in the CFBundleTypeIconFile key in the CFBundleDocumentTypes array, not in the UTExportedTypeDeclarations array.

And of course make sure that "My-file-icon.icns" is in your target's Copy Bundle Resources build phase and is being copied into Contents/Resources in your app's bundle.

I've been trying to do this in Xcode 11 and it's been a challenge. There are lots of answers here on SO to questions like this but I couldn't find one that had everything I needed to get this working. I'll try to do that here.

Technical Q&A QA1587 Although this is a bit old I think it has everything required but it's a bit vague in some spots like when it says Provide an icon for the document.

I thought I could make it work with an icon in Assets.xcassets but it didn't work for me until I provided an icns file. I took the 1024x1024 png app icon and converted it to an icns here at cloudconvert . Then I added that icns to my project. I don't think it matters where you put it. I put it just below the top level. It's circled in the image below.

在此处输入图片说明 Follow the arrows in the image and you end up at the Info tab. I tapped the Add button to create a Document Type and filled it in as shown. When you tap the Add button in the icon section it will ask you for an icns file. Once I had mine in the project it appeared in the window and I could select it.

Same for Configured UTIs.

That was it. In Files, Messages, or Mail the icon appears.

I also had a similar problem, turns out I needed to rebuild the launch services database as per what i have documented here....

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

Your UTI declaration in the Info.plist appears to be correct, however I noticed an other issue. If you application is a document-based application you need to replace the NSString in following entry with your NSDocument subclass:

<key>NSDocumentClass</key>
<string>NSString</string>

For example it's "SKTDocument" in Sketch:

<key>NSDocumentClass</key>
<string>SKTDocument</string>

Edit: Please also make sure to use your own reverse domain name for your exported UTIs. This ensures that UTIs are unique. For example its com.mindnode.MindNode.MindNodeDocument in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM