Open any file in Cocoa
My Cocoa (Mac) application deals with a file format that doesn't have a standardised file extension and the number one request I receive from my users is to allow opening files with any file extension, not just a closed set of the most common ones I have prepared.
Normally you specify what kind of files you want to open in the Info.plist by declaring the UTIs your classes handle and declaring imported or exported UTIs as needed. You can specify a list of file extensions there that will be associated with those UTIs. Files with extensions not listed there will not be droppable to your application icon in your dock and will be greyed out in the Open File dialog. I can't seem to find a way to override this behavi开发者_JAVA技巧our and accept files with any extensions. There are various sites on the Internet that suggest assigning * to the file extension, but that doesn't seem to be working for me.
There are a number of Cocoa apps in the Mac App Store that can do that, some of them from Apple (e.g. Xcode), some made by third party developers (TextWrangler) so I assume there must be a legit way to achieve this.
TLDR: How to accept files with any extension in Cocoa?
I have this set up in one of my apps. It's defined with the following value for Info.plist Key 'CFBundleDocumentTypes':
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>*</string>
</array>
<key>CFBundleTypeMIMETypes</key>
<array>
<string>public.item</string>
</array>
<key>CFBundleTypeOSTypes</key>
<array>
<string>****</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSItemContentTypes</key>
<array>
<string>public.item</string>
</array>
<key>LSTypeIsPackage</key>
<false/>
<key>NSDocumentClass</key>
<string>MONDocumentClass</string>
</dict>
</array>
but you may need to modify that.
精彩评论