Settings.bundle の Root.plist から Title, Value を持つ Dictionary の配列をロケールに合わせて取得する

September 8, 2010

Settings.bundle の Root.plist をアプリケーションに作成すると、iPhone/iPad の「設定」画面とそれによるパラメータ設定が可能になりますが、その中の PSGroupSpecifier の候補リストをアプリケーション内部から取得する方法について明記します。

設定値は、iOS のシステムロケールによって適切な Title に置き換わります。そこも同様になるように処理します。

Settings.bundle の Root.plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>SearchTarget</string>
</dict>
<dict>
<key>Values</key>
<array>
<string></string>
<string>ja_JP</string>
<string>en_US</string>
<string>en_GB</string>
</array>
<key>Titles</key>
<array>
<string>SystemLocale</string>
<string>Japan</string>
<string>USA</string>
<string>UK</string>
</array>
</dict>
</array>
</dict>
</plist>

PreferenceSpecifiers / PSGroupSpecifier の取得処理

Settings.bundleの取得

まず、Settings.bundle への参照を取得します。

1
2
3
NSString *settingBundlePath = \[\[\[NSBundle mainBundle\] bundlePath\]
stringByAppendingPathComponent:@"Settings.bundle"\];
NSBundle *settingBundle = \[NSBundle bundleWithPath:settingBundlePath\];
Root.plist の取得

NSBundle の pathForResource:ofType: メソッドで Root.plist を NSDictionary のインスタンスとして取得します。

1
2
NSString *rootPlistPath = \[settingBundle pathForResource:@"Root" ofType:@"plist"\];
NSDictionary *rootPlist = \[NSDictionary dictionaryWithContentsOfFile:rootPlistPath\];
PreferenceSpecifiers の値候補リストを取得

PreferenceSpecifiers は二つの要素を持ち、インデックス0は定義、インデックス1が実体なので後者の NSDictionary を取得します。
さらに値と表記のそれぞれの要素群を Values, Titles を NSArray で取得します。

1
2
3
4
NSArray *array = \[rootPlist objectForKey:@"PreferenceSpecifiers"\];
NSDictionary *localeItem = \[array objectAtIndex:1\];
NSArray *values = \[localeItem objectForKey:@"Values"\];
NSArray *titles = \[localeItem objectForKey:@"Titles"\];

Values, Titles の各インデックス値をペアとして NSDictionary を生成し、NSArray にまとめます。

1
2
3
4
5
6
7
8
9
10
11
int count = \[values count\];
NSMutableArray *items = \[\[NSMutableArray alloc\] initWithCapacity:count\];
for (int i = 0; i < count; i++) {
NSMutableDictionary *newItem = \[NSMutableDictionary new\];
\[newItem setObject:\[values objectAtIndex:i\] forKey:@"value"\];
NSString *titleKey = \[titles objectAtIndex:i\];
NSString *localizedTitle = \[settingBundle localizedStringForKey:titleKey value:titleKey table:@"Root"\];
\[newItem setObject:localizedTitle forKey:@"title"\];
\[items addObject:newItem\];
\[newItem release\];
}

Title のロケール反映値は NSBundle の localizedStringForKey:value:table: メソッドを利用して取得している。
上記の items を UITableView などで利用できる。

tilfin freelance software engineer