I save app data to the Documents folder for restore between app launches, but what happens if the app is updated with a new version? Is this data preserved or just lost?
Do I need to enable the feature to allow the user to backup the data manually via the iTunes app as a workaround?
NSUserDefaults works between updates and is how you should save preferences and other data that aren’t documents/files. It’s also available in the Foundation module of iOSKit.
NSUserDefaults (or AppleUserDefaults in iOSLib) is pretty straightforward to work with. The only “gotcha” is to make sure you call the synchronize method after you save/change any values because they will not be saved automatically unless you synchronize.
Jason are you sure about this? I’ve been using your NSUserDefaults since it first appeared in iOSKIt and I’ve never called, nor needed to call, the synchronize method.
Well synchronize is called automatically by iOS periodically after a change. But if the app quits before it is called then the changes will be lost. So although it works, you could come across a situation where it is not saved (I came across this when testing it originally) and this is why I say that.
I see. I only use it for settings in a series of views similar to the iOS Settings app, and as these are rarely changed then presumably nobody has experienced this issue. I’ll update it to call synchronize when leaving the views.
That sounds like you should be fine. Its most important if you are saving something in response to the app quitting where you will lose changes. The apple docs are fairly shady on this.
Documents folder remain untouched and all the backup will save it.
Cache folder should be emptied and is not part of the backup
NSUserDefault should be used for preference and settings, not for data.
Derk Jochems said [quote]I’ve tested the working of the AppleUserDefaults in (iOSLib) it works good.
It’s undocumented for now so if you need help call out.[/quote]
Jason King said [quote]NSUserDefaults (or AppleUserDefaults in iOSLib) is pretty straightforward to work with.[/quote]
I spent a many hours trying to figure out how to make NSUserDefaults work with iOSLib. I just don’t have the knowledge to do it. I got stopped when trying to create the defaults Dictionary. If someone would post sample code there would probably be many grateful developers, myself among them.
if it might help anyone… here is the SWIFT code I wrote for loading/saving optinos
let userDefaults = NSUserDefaults.standardUserDefaults()
func putOption(key:String,_ optionValue:Any) {
switch optionValue {
case let someBool as Bool:
userDefaults.setBool(someBool, forKey: key)
case let someDouble as Double:
userDefaults.setDouble(someDouble, forKey: key)
case let someFloat as Float:
userDefaults.setFloat(someFloat, forKey: key)
case let someInt as Int:
userDefaults.setInteger(someInt, forKey: key)
case let someString as String:
userDefaults.setObject(someString, forKey: key)
case let someColor as UIColor:
userDefaults.setInteger(Int(someColor.IntValue()), forKey: key)
default:
break
}
}
func getOption(key:String, defaultValue:AnyObject) -> AnyObject {
if let retValue:AnyObject = userDefaults.objectForKey(key) {
return retValue
} else {
putOption(key,defaultValue) // insure the option exists in pList
return defaultValue
}
}
(For iOSKit) you call NSUserDefaults.StandardUserDefaults. This gives you the “dictionary” to work with. Then you can call the different methods to save/restore values. For example SetDoubleForKey sets a double value for a string/text key as the name implies. The other functions work the same. If you have more specific questions, just ask.