I have created an app with settings bundle in it and the app configuration can be changed. Reading from Settings bundle is done. I am using below code.
let defs: NSUserDefaults = NSUserDefaults.standardUserDefaults()
defs.synchronize()
var settingsBundle: NSString = NSBundle.mainBundle().pathForResource("Settings", ofType: "bundle")!
if(settingsBundle.containsString("")){
NSLog("Could not find Settings.bundle");
return;
}
var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))!
var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray
var defaultsToRegister: NSMutableDictionary = NSMutableDictionary(capacity: preferences.count)
for prefSpecification in preferences {
if (prefSpecification.objectForKey("Key") != nil) {
let key: NSString = prefSpecification.objectForKey("Key")! as! NSString
if !key.containsString("") {
let currentObject: AnyObject? = defs.objectForKey(key as! String)
if currentObject == nil {
// not readable: set value from Settings.bundle
let objectToSet: AnyObject? = prefSpecification.objectForKey("DefaultValue")
var objectKey : String = key as! String
var finalObject : String = objectToSet as! String
defaultsToRegister.setObject(finalObject, forKey: objectKey)
}else{
//already readable: don't touch
var objectKey : String = key as! String
var finalObject : String = currentObject as! String
defaultsToRegister.setObject(finalObject, forKey: objectKey)
}
}
}
}
defs.registerDefaults(defaultsToRegister as [NSObject : AnyObject])
defs.synchronize()
The same settings can be updated the app itself. But how I can write the updated values into the Settings bundle?
In this link, the accepted answer says it is possible using NSUserDefaults
. But I couldn't, someonce can please help?
EDIT Please check below for more info:
{
PreferenceSpecifiers = (
{
Title = Group;
Type = PSGroupSpecifier;
},
{
DefaultValue = Test1;
Key = "multi_values";
Title = "Multiple Values";
Titles = (
FIrst,
Second,
Third
);
Type = PSMultiValueSpecifier;
Values = (
FirstValue,
SecondValue,
ThirdValue
);
}
);
StringsTable = Root;
}
I am having a Settings bundle like above format and I am able to read it in the app. But how can I update this multi values selection as per I make the changes in the app? I used the following code to update the Title value.
var settings: NSDictionary = NSDictionary(contentsOfFile: settingsBundle.stringByAppendingPathComponent("Root.plist"))!
var preferences: NSArray = settings.objectForKey("PreferenceSpecifiers") as! NSArray
preferences.objectAtIndex(1).setValue("MutipleValue is updated", forKey: "Title")
settings.setValue(preferences, forKey: "PreferenceSpecifiers")
settings.writeToFile(settingsBundle.stringByAppendingPathComponent("Root.plist"), atomically: false)