I want to create in-app settin开发者_运维技巧gs for my iPad application. There are different alerts for different groups, so the user can select which alerts he/she wants in the application. I am putting a custom button which will look like checkbox so when user will click it, it will be highlighted.
Can anyone tell me where can I store the settings, do I need to save settings in keychain or somewhere else? Is there any tutorial for doing that?
I've found NSUserDefaults
very helpful for stuff like this. Basically you do this to store values:
NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
[defaults setBool:yourBool forKey:@"yourBoolKey"];
[defaults setInteger:yourInteger forKey:@"yourIntegerKey"];
[defaults synchronize];
And to get values:
NSUserDefaults* defaults= [NSUserDefaults standardUserDefaults];
yourBool= [defaults boolForKey:@"yourBoolKey"];
yourInteger= [defaults integerForKey:@"yourIntegerKey"];
To have default settings when the app first launches, you could just check a bool with the key @"AppHasStoredSettings"
or something like that, which will be NO
the first time, set your default settings, and then set it to YES
.
The best thing would probably be a plist, which, in reality is a structured XML file with keys and values, but Apple abstracts most of that for you pretty well with some nice settings tools. Here's a few things to peruse to get a handle on the idea:
- NSUserDefaults
- User Defaults Programming Topics
- Information about property list files
Luke put some helpful code, but look at these too for more examples and ways to use all the tools available.
精彩评论