How to save user settings for my iPhone application?

The title of the question pretty much gives it away - I want my app to remember a few things. This is some kind of calculator, so it should save the last used values ​​and some user settings.

Basically, I would like to save some float and BOOLs and load them again the next time the application loads.

What is the best and easiest way to do this?

Thank!!

+43
ios objective-c iphone
Aug 25 '10 at 13:11
source share
5 answers

One of the easiest ways is to save it to NSUserDefaults :

Installation:

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:value forKey:key]; // – setBool:forKey: // – setFloat:forKey: // in your case [userDefaults synchronize]; 

Receiving:

 [[NSUserDefaults standardUserDefaults] objectForKey:key]; – boolForKey: 

and

– floatForKey: in your case.

+125
Aug 25 '10 at 13:15
source share

Besides the very good approach of NSUserDefaults, there is another easy way to store data from NSArray, NSDictionary, or NSData in a file. You can also use these methods:

 - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag 

accordingly (for NSDictionary):

 + (id)dictionaryWithContentsOfFile:(NSString *)path 

you just need to provide a valid location path. According to the iOS Application Programming Guide, the / Library / Caches directory will be the best place to store the data that you need to keep between application launches. (see here )

To store / load a dictionary from a file called "managers" in your directoy document, you can use the following 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]; } } 

In any case, this approach is very limited and you need to spend a lot of extra work if you want to store more complex data. In this case, the CoreData API is very convenient.

+6
Aug 25 '10 at 13:46
source share

Are you looking for NSUserDefaults

+2
Aug 25 '10 at 13:13
source share

In Swift:

Customization

 let userDefaults = NSUserDefaults.standardUserDefaults() userDefaults.setObject(value, forKey: key) // userDefaults.setFloat(12.34, forKey: "myFloatKey") // userDefaults.setBool(true, forKey: "myBoolKey") 

Please note that for iOS 8 and later, calling userDefaults.synchronize() not recommended .

Getting

 let userDefaults = NSUserDefaults.standardUserDefaults() if let value = userDefaults.objectForKey(key) { print(value) } 

Note that userDefaults.boolForKey and userDefaults.floatForKey return optional values, so they will never be nil (only false or 0.0 ).

Further reading

+2
Sep 18 '15 at 2:09
source share

Swift 4 / Linux

Apparently, something has changed. Now there is a class UserDefault . Check out these links:

https://developer.apple.com/documentation/foundation/userdefaults

https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-userdefaults

0
Oct 10 '17 at 10:06 on
source share



All Articles