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?
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With