我可以通过应用程序修改设置中的Root.plist捆绑PSMultiValueSpecifier标题和值

时间:2012-07-30 07:44:15

标签: objective-c settings.bundle root.plist

我想通过我的应用程序更改PSMultiValueSpecifier标题数组和值数组。这是我的Root.plist文件。

<?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>PreferenceSpecifiers</key>
<array>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Login Information</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
        <key>Title</key>
        <string>Restaurant Code</string>
        <key>Key</key>
        <string>Restaurant_Code_key</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
        <key>Title</key>
        <string>Password            </string>
        <key>Key</key>
        <string>Password_key</string>
        <key>IsSecure</key>
        <true/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Remember Login Information</string>
        <key>Key</key>
        <string>Remember_Login_Information_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string>Menu Content</string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSMultiValueSpecifier</string>
        <key>Title</key>
        <string>Default Language</string>
        <key>Key</key>
        <string>Default_Language_key</string>
        <key>DefaultValue</key>
        <integer>0</integer>
        <key>Titles</key>
        <array>
            <string>English</string>
            <string>Spanish</string>
        </array>
        <key>Values</key>
        <array>
            <integer>0</integer>
            <integer>1</integer>
        </array>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Update Data on Launch</string>
        <key>Key</key>
        <string>Update_Data_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSGroupSpecifier</string>
        <key>Title</key>
        <string></string>
    </dict>
    <dict>
        <key>Type</key>
        <string>PSToggleSwitchSpecifier</string>
        <key>Title</key>
        <string>Reset on Next Launch</string>
        <key>Key</key>
        <string>Reset_key</string>
        <key>DefaultValue</key>
        <false/>
        <key>TrueValue</key>
        <true/>
        <key>FalseValue</key>
        <false/>
    </dict>

</array>
<key>StringsTable</key>
<string></string>
</dict>
</plist>


I tried to change like this Through my application.
<dict>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Default Language</string>
<key>Key</key>
<string>Default_Language_key</string>
<key>DefaultValue</key>
<integer>0</integer>
<key>Titles</key>
<array>
<string>English</string>
</array>
<key>Values</key>
<array>
<integer>0</integer>
</array>
</dict>

但这不会改变标题和价值。请有人帮助我。下面给出了我用过的代码。

NSURL * settingsURL =  [[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"bundle"]]
                        URLForResource:@"Root" withExtension:@"plist"];
NSMutableDictionary * settingsDict = [NSMutableDictionary    dictionaryWithContentsOfURL:settingsURL];
NSMutableArray *settingsArr = [settingsDict objectForKey:@"PreferenceSpecifiers"];

NSMutableDictionary *loTempDict = [[NSMutableDictionary alloc] init];
loTempDict = [settingsArr objectAtIndex:5];

NSArray *title_Array = [loTempDict objectForKey:@"Titles"];
NSArray *value_Array = [loTempDict objectForKey:@"Values"];

NSArray *lanArray_title = [NSArray arrayWithObject:@"French"];
NSArray *lanArray_value = [NSArray arrayWithObject:@"0"];

[loTempDict setObject:lanArray_title forKey:@"Titles"];
[loTempDict setObject:lanArray_value forKey:@"Values"];
[settingsArr replaceObjectAtIndex:5 withObject:loTempDict];


NSLog(@"final settingsArr ===%@ ",settingsArr);

NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

[settingsArr writeToFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"] atomically: TRUE];

由于

2 个答案:

答案 0 :(得分:0)

你不能这样做。出于安全原因,无法写入应用程序包。您可以提出的最佳替代方法是将要修改的文件从应用程序包目录复制到库或文档目录 - 这些是可写的,并且可以更新其中的文件。

答案 1 :(得分:-1)

这将解决您的问题

NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];

[settingsDict setValue:settingsArr forKey:@"PreferenceSpecifiers"];

[settingsDict writeToFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"] atomically: TRUE];