The question title pretty much gives it away - I'd like my app to remember a few things. It's some sort of calculator, so it should save the last used values and some user selectable settings.
Basically I'd like to save a handful of floats and BOOLs and load them again the next time the app loads.
What's the best and easiest way to do that?
Thanks!!
Go to Settings > App Store, then do any of the following: Automatically download apps purchased on your other Apple devices: Below Automatic Downloads, turn on Apps. Automatically update apps: Turn on App Updates. Allow app downloads to use cellular data: Below Cellular Data, turn on Automatic Downloads.
Go to Settings > Privacy, then tap App Privacy Report (iOS 15.2 or later). The App Privacy Report shows you how apps are using the permissions you granted them and shows you their network activity.
One of the easiest ways would be saving it in the NSUserDefaults
:
Setting:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:value forKey:key]; // – setBool:forKey: // – setFloat:forKey: // in your case [userDefaults synchronize];
Getting:
[[NSUserDefaults standardUserDefaults] objectForKey:key]; – boolForKey:
and
– floatForKey:
in your case.
Besides the very good NSUserDefaults approach, there is another easy way to store data from an NSArray,NSDictionary or NSData in a file. You can use these methods as well:
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
respectively (for a NSDictionary):
+ (id)dictionaryWithContentsOfFile:(NSString *)path
you just have to give a valid path to a location. According to the iOS Application Programming Guide, the /Library/Caches directory would be the best place to store data that you need to persist between app launches. (see here)
In order to store/load a dictionary from a filed called "managers" in your document directoy you could use these methods:
-(void) loadDictionary { //get the documents directory: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; //create a destination file name to write the data : NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory]; NSDictionary* panelLibraryContent = [NSDictionary dictionaryWithContentsOfFile:fullFileName]; if (panelLibraryContent != nil) { // load was successful do something with the data... } else { // error while loading the file } } -(void) storeDictionary:(NSDictionary*) dictionaryToStore { //get the documents directory: NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; //make a file name to write the data to using the //cache directory: NSString *fullFileName = [NSString stringWithFormat:@"%@/managers", cacheDirectory]; if (dictionaryToStore != nil) { [dictionaryToStore writeToFile:fullFileName atomically:YES]; } }
Anyway this approach is very limited and you have to spend a lot of extra work if you want to store more complex data. In that case the CoreData API is very very handy.
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