シナリオ:
Info.plist
Cocoa アプリケーションのファイルで許可されるファイル タイプ (コンテンツ タイプ) を定義したいと考えています。したがって、次の例のように追加しました。
# 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
か?
注:
調査を通じて、この記事は非常に有益であることがわかりました: Simplifying Data Handling with Uniform Type Identifiers。