I have tried the various techniques described to serialize a plist I need in order to save the favorites of the user.
Some people also say it is better to save that data into the "user defaults" file.
Which is the better technique and how are they serialized. I have this code
[plistArray writeToFile:filepath atomically: YES];
which adds the object to the array, but cant seem to save the plist edits.
Regards
If you want to save preferences, then the appropriate method is to use the NSUserDefaults mechanism as it will save you a hell of a lot of time and hassle.
However, if you're using a PLIST as a means of persisting data that's stored in say, an NSDictionary, then I think it would be appropriate to use the writeToFile approach.
If you want to save preferences of any kind, just store them in the user defaults dictionary. That's the only correct way – and additionally you make sure, that iTunes will back it up, if the user syncs its phone.
Related
[NSPropertyListSerialization propertyListFromData:self.responseData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
When the server is online, it sends the responseData. Now I want to save this data(obtained through the responseData in the code) directly to another plist in my app, so that I can retrieve the data later(when the server is offline) in the same format(without changing) and use it/pass it in the same way as it is being passed when my server is online. I am doing this so that the user of the app does not come to know whether the server is online/offline and still is able to give updates/feedback to the responseData (as he/she would give when the server is online). I don't want to break the responseData and save it, rather directly save it the way it is coming from the server.
Please let me know if more explanation is required.And thanks for all the answers that you would be giving :)
If you don't want to change anything I believe you should just download the plist as a file and not as data.
To save the file on the phone you should use NSFileManager, you can read about it here:NSFileManager reference + File System Programming Guide
You must pay attention to the place you save the plist according to apple "new" rules of data storage, you can read about it here:
Were you should save your files
Good luck
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?
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.
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;
}
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.
In my app, I am writing some data, and I want to save it so that I can retrieve it later. How can I save it and where will it be saved?
Please suggest some ideas...
I think you want to store it in a database using Core Data. If you give more specifics I may be able to give a more specific answer.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/cdProgrammingGuide.html
NSUserDefaults would be the obvious suggestion.
Other suggestions would be to write it to a .plist file in the documents directory or use core-data depending on how much you would want to save.
I am trying to save drafts using NSUserDefaults but I am not sure if it is the right place to save data not user's preferences in.
My application allows user to choose 1 or 2 images to upload and share. Users can save draft and come back to upload later. My current approach is to save the chosen UIImage to /Library directory and save the file path to NSUserDefaults.
The data structure is NSUserDefaults -> Draft NSDictionary -> key - NSArray. I have many drafts (just like email drafts). The Draft Dictionary contains a saved date string as a key and string file paths in NSArray. The saving process doesn't have any problems currently. But because in NSUserDefaults doc, they say:
The defaults system allows an
application to customize its behavior
to match a user’s preferences.to match a user’s preferences.
So, I don't know if my approach is good or not, any possible future problems with it?
Do you suggest me any simple solution that can do this well without having to code much. I know there is CoreData but I don't know much about it. Is it easy to learn and implement?
I don't know if this can affect some memory problem or not, but I expect that a user may save drafts less than 100.
You don't want to save any actual data to NSUserDefaults, however, it acceptable to save the previous application state in the defaults such that the app can resume where it left off.
Your data structure seems overly complex. You can just save an array of file paths directly to the user defaults.
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
NSArray *draftPaths=[NSArray arrayWithObjects:filePath1, filePath2,nil];
[defaults setObject:draftPaths forKey:#"DraftPaths_Key"];
// to retrieve
NSArray *previousDraftPaths=[defaults objectForKey:#"DraftPaths_Key"];
You don't want to save the files into the cache folders because the system can delete caches if space gets tight. Instead, save them to custom folder in the /Library directory. You can get the path to the library with:
NSArray *libraryPaths=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libPath=[libraryPaths objectAtIndex:0];
I want to store some user settings like selected image, etc.
How could I do that with an plist?
For basic settings you can use NSUserDefaults. If you need something more complicated (serializing objects) then you should write out and read in a data file.