9

シナリオ:

Info.plistCocoa アプリケーションのファイルで許可されるファイル タイプ (コンテンツ タイプ) を定義したいと考えています。したがって、次の例のように追加しました。

# 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())使えます。


質問:

  1. ファイルからコンテンツ タイプ情報を抽出するよりスマートな方法を知っていInfo.plistますか? Cocoa 組み込み機能はありますか?
  2. そこに含めることができるフォルダーの定義をどのように処理しますpublic.folderか?

注:
調査を通じて、この記事は非常に有益であることがわかりました: Simplifying Data Handling with Uniform Type Identifiers

4

1 に答える 1

1

plistから情報を読み取る方法は次のとおりです(正しいパスを設定していれば、プロジェクトにあるinfo.plistまたはその他のplistにすることができます)

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *fullPath = [NSString stringWithFormat:@"%@/path/to/your/plist/my.plist", resourcePath];
NSData *plistData = [NSData dataWithContentsOfFile:fullPath];
NSDictionary *plistDictionary = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:0 errorDescription:nil];
NSArray *fileTypes = [plistDictionary objectForKey:@"CFBundleDocumentTypes"];
于 2011-11-05T01:11:17.090 に答える