I know this sounds like an odd question, but I need to keep a copy of my NSUserDefaults in to a database (my aim is provide a database backup / restore feature, using one file, the database).
So I think I’ve figured out how to load to a file (although I haven’t tried this in xcode).
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults registerDefaults:[NSDictionary dictionaryWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"]]];
I’ve googled how to save NSUserDefaults to a plist and to a string and back, but haven’t found anything.
You can use the asynchronous NSPropertyListSerialization API or just the synchronous convenience methods on NSDictionary.
Checkout the discussion in the NSDictionary Apple Docs on the writeToFile:automatically method for more info on how it works
Also, This article has some good info on Serialization in cocoa generally.
Use the following code should get you on your way.
When you want to go backwards you can simply do something like:
Or you could use the stream/NSData approach from NSPropertyListSerialization, which is similar to the way you save it.