I am writing an iPhone application, and need to save the state of my application (5K or so).
My main worry is data persisting across upgrades. Some of the applicatio
I use sqlite to store all application data, and preferences. To make sure that updates do not wipe the data, make sure the sqlite file is stored in the Documents directory of the application, which is not overwritten by upgrades. Some of the example code ("SQLite Books" I think) Apple provides has code to handle this.
Here is a link to a discussion that provided a very good example of how to access your data and which type of storage schemes would be more beneficial under certain circumstances. @Ben Gottlieb's solution is on target, I just thought this question would add an additional resource. Hope this helps!
To save state, NSUserDefaults is the way to go. I believe most, if not all, instances of application data being deleted after an upgrade are due to issues on the AppStore. They may be related to data-format changes, but if you use just NSUserDefaults and standard plist-storable objects (NSString, NSDictionary, NSNumber, NSArray, NSNumber, and primitives), you should be safe.
It really depends on what you're doing and your needs.
If it's a small amount of data NSUserDefaults is a great way to go. You can also save a dictionary or an array to your apps Documents folder, then load that file back in on launch.
SQLite is nice, but unless you've got a lot of data that you'll be querying, it's a little over the top for just saving state.
for small amounts of data NSUserDefaults is a perfect solution, if you need ad hoc querying of data sqlite is the way to go. Those two solutions are perfect for storing data on the device, if you need more flexibility and capability you could consider remote data i.e web services and a server database.
There's a simply way to do exactly this To save:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@"TextToSave" forKey:@"keyToFindText"];
To load:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *textToLoad = [prefs stringForKey:@"keyToFindText"];
The important thing is that you do not store very large values in NSUserDefaults
. There's a chance of getting in trouble with apple review if you do.
Generally, user defaults is used for storing short keys and preferences such as the users volume level, wether they want to use game center, or send crash logs.