Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the options for saving data in iOS?

Tags:

I'd like to serialize a bunch of data and save it to a file, then be able to load it (naturally) and play it back with a feature I have written. (Sorry if my terminology is off, I am a novice with this sort of thing.) What is the best way to do this with iOS? From looking at documentation here:

Standard Application Behaviors Guide

I've gathered that I should use NSSearchPathForDirectoriesInDomains for finding the appropriate storage directory root (Documents?) and then use NSData to store these data buffers I create and write them to file. Am I spot on with that or have I got it wrong? Any other sage advice?

like image 558
Joey Avatar asked Nov 02 '10 05:11

Joey


People also ask

What is the best way to save data on iPhone?

With iOS 13 and later, you can turn on Low Data Mode to restrict background network use and save cellular and Wi-Fi usage. You might want to use Low Data Mode if your cellular or internet plan limits your data usage, or if you're in an area with slow data speeds.

Where do iOS apps store data?

iOS apps store data locally in different ways like plist(similar to shared_pref in android), NSUserDefaults, Core data(sqlite), Keychain. Theses files can be found using any file explorer utility under the application folder.


1 Answers

You can use plist.
It is very easy to save list of string or other data without using core data.
See Property List Programming Guide

For example, this code will save an object (rootObj) which can be an array or a dictionary:

NSString *error; NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [rootPath stringByAppendingPathComponent:@"yourFile.plist"]; NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:rootObj                                                                format:NSPropertyListXMLFormat_v1_0                                                      errorDescription:&error]; if(plistData) {   [plistData writeToFile:plistPath atomically:YES]; } else {   NSLog(@"Error : %@",error);   [error release]; } 

There is some limitation on supported class (see the guide)

like image 98
Benoît Avatar answered Sep 25 '22 18:09

Benoît