在Settings.bundle中向PSMultiValueSpecifier添加选项

时间:2010-11-10 00:41:13

标签: iphone settings.bundle

我一直在尝试使用Settings.bundle作为使用PSMultiValueSpecifier的选项存储,这将允许用户选择其中一个。所以我需要能够在Settings.bundle中添加新选项。看似很容易。

以下是Root.plist中的PSMultiValueSpecifier示例

<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Options</string>
<key>DefaultValue</key>
<string></string>
<key>Key</key>
<string>optionSelection_preference</string>
<key>Values</key>
<array>
    <string>option_1_key</string>
</array>
<key>Titles</key>
<array>
    <string>Option 1</string>
</array>

所以我试图添加'选项2',这是关键。所以我写了以下代码(部分):

NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle];

if (rootPlist == nil)
    return NO;

NSArray* specifiers = [rootPlist objectForKey:@"PreferenceSpecifiers"];

NSMutableDictionary *multiValueSpecifier = nil;

for (NSMutableDictionary *specifier in specifiers)
    {
    if ([[specifier objectForKey:@"Key"] isEqualToString:speficierKey] == YES && [[specifier objectForKey:@"Type"] isEqualToString:@"PSMultiValueSpecifier"] == YES)
        {
        multiValueSpecifier = specifier;
        break;
        }
    }

if (multiValueSpecifier == nil)
    return NO;

NSMutableArray* titlesArray = [[multiValueSpecifier objectForKey:@"Titles"] mutableCopy];
NSMutableArray* valuesArray = [[multiValueSpecifier objectForKey:@"Values"] mutableCopy];

[titlesArray addObject:title];
[valuesArray addObject:value];

[multiValueSpecifier setObject:titlesArray forKey:@"Titles"];
[multiValueSpecifier setObject:valuesArray forKey:@"Values"];

return [rootPlist writeToFile:settingsBundle atomically:YES];

NSDictionary* rootPlist = [NSDictionary dictionaryWithContentsOfFile:settingsBundle]; if (rootPlist == nil) return NO; NSArray* specifiers = [rootPlist objectForKey:@"PreferenceSpecifiers"]; NSMutableDictionary *multiValueSpecifier = nil; for (NSMutableDictionary *specifier in specifiers) { if ([[specifier objectForKey:@"Key"] isEqualToString:speficierKey] == YES && [[specifier objectForKey:@"Type"] isEqualToString:@"PSMultiValueSpecifier"] == YES) { multiValueSpecifier = specifier; break; } } if (multiValueSpecifier == nil) return NO; NSMutableArray* titlesArray = [[multiValueSpecifier objectForKey:@"Titles"] mutableCopy]; NSMutableArray* valuesArray = [[multiValueSpecifier objectForKey:@"Values"] mutableCopy]; [titlesArray addObject:title]; [valuesArray addObject:value]; [multiValueSpecifier setObject:titlesArray forKey:@"Titles"]; [multiValueSpecifier setObject:valuesArray forKey:@"Values"]; return [rootPlist writeToFile:settingsBundle atomically:YES];

代码似乎有效,我在Settings.bundle中看到了更改。

问题:重新加载时,新条目消失了,只有从XCode添加的条目仍然存在。

1 个答案:

答案 0 :(得分:3)

无论如何,这在App Store应用程序中无效。您无法修改“设置”捆绑包,因为这会修改App Store应用程序不允许的应用程序包的内容,因为它会使其签名无效。