Reputation: 442
I'm trying to implement a backup of my application's data and user preferences (stored in NSUserDefaults) as email attachments with option to restore them at a later date.
I've got the process working fine for my application data file simply by attaching the contents of the file to the email, but can't work out how to do the equivalent for the user preferences. The Root.plist in the Settings bundle contains only the template for the settings interface and none of the current settings.
Reading the settings into my own plist and saving that to the documents directory is an option but seems inelegant and overly complicated. Is there a better way?
Upvotes: 4
Views: 976
Reputation: 13388
Reading the settings into my own plist and saving that to the documents directory is an option but seems inelegant and overly complicated. Is there a better way?
Given that there is no official API (that I know of) that directly supports what you want, I find it quite elegant and not very complicated to write a few lines of code that create your own .plist file. See yuji's answer for a starting point: Just one line and you already have a dictionary with all the settings that you want. How much more elegant can it get? :-)
It may not be the answer you would have liked to hear, but my advice is: Don't try to fight the system, you usually lose in the long run.
Upvotes: 1
Reputation: 16725
I wouldn't go looking for the plist that stores NSUserDefaults
, because it's not directly exposed by the API, and hence is an implementation detail that could be changed at any time.
Instead,
[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
will give you an NSDictionary
containing all the key-value pairs that your app has stored.
Upvotes: 5
Reputation: 4915
See this tutorial,
http://iphonebyradix.blogspot.in/2011/03/read-and-write-data-from-plist-file.html
To read user defaults , use this method
-(id)getFromNSUserDefaults:(NSString*)pForKey
{
id pReturnObject;
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
pReturnObject = [defaults valueForKey:pForKey];
return pReturnObject;
}
Upvotes: 1