场景:
我想在我的Cocoa应用程序的Info.plist
文件中定义允许的文件类型(内容类型)。因此,我按照以下示例将它们添加。
# Extract from Info.plist
[...]
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>public.png</string>
<key>CFBundleTypeIconFile</key>
<string>png.icns</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSIsAppleDefaultForType</key>
<true/>
<key>LSItemContentTypes</key>
<array>
<string>public.png</string>
</array>
</dict>
[...]
此外,我的应用程序允许使用
NSOpenPanel
打开文件。该面板可以通过以下选择器设置允许的文件类型: setAllowedFileTypes:
。 文档指出UTI可用。
文件类型可以是常见的文件扩展名,也可以是UTI。
一种自定义解决方案:
我编写了以下帮助方法,从 Info.plist
文件中提取 UTI。
/**
Returns a collection of uniform type identifiers as defined in the plist file.
@returns A collection of UTI strings.
*/
+ (NSArray*)uniformTypeIdentifiers {
static NSArray* contentTypes = nil;
if (!contentTypes) {
NSArray* documentTypes = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDocumentTypes"];
NSMutableArray* contentTypesCollection = [NSMutableArray arrayWithCapacity:[documentTypes count]];
for (NSDictionary* documentType in documentTypes) {
[contentTypesCollection addObjectsFromArray:[documentType objectForKey:@"LSItemContentTypes"]];
}
contentTypes = [NSArray arrayWithArray:contentTypesCollection];
contentTypesCollection = nil;
}
return contentTypes;
}
除了使用[NSBundle mainBundle]
,也可以使用CFBundleGetInfoDictionary(CFBundleGetMainBundle())
。
问题:
- 你是否知道从
Info.plist
文件中提取内容类型信息的更智能的方法?是否有Cocoa内置函数? - 如何处理可能包含在其中的文件夹的定义,例如
public.folder
?
注:
在我的研究过程中,我发现这篇文章非常有用:使用统一类型标识符简化数据处理。
NSPropertyListSerialization
。不过,你的解决方案需要知道 plist 文件的路径。- 那我的问题的第二部分呢? - JJDpublic.image
)和文件夹(public.folder
)指定UTI。如何通过“文件/打开”和“拖放到应用程序图标上”允许在您的应用程序中打开图像(单个/多个/文件夹)?请避免冗余的UTI定义。 - JJD