Can i modify Root.plist in settings bundle PSMultiValueSpecifier titles and values through the application

StackOverflow https://stackoverflow.com/questions/11717274

I want to change the PSMultiValueSpecifier title array and value array through my application .This is my Root.plist file.

<?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>

But this will not change the title and values.Please some one help me .The code is given below that i used.

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];

Thanks

没有正确的解决方案

其他提示

You can't do this. Writing to the app bundle is not possible for security reasons. The best alternative you can come up with is to copy the file to be modified from the app bundle directory to the Library or Documents directory - these are writable and the files inside them can be updated.

This will solve your problem

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

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

[settingsDict writeToFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"] atomically: TRUE];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top