I want to ship my iPhone app with some preferences but I don't need to create a "Settings App" using a plist and all that. I would like to do this using NSUserDefaults and know 开发者_如何学Gohow to store and retrieve using this class. However, I'm struggling with how to have an initial set of preferences there when the user loads the app for the first time. Should I retrieve my NSUserDefaults in ViewDidLoad and if they return nil, set them at that point? Or is there a better method?
I'd do it like you proposed. Check whether the user default keys do exist already and if not, create them using default values.
Where you do this check is up to you, but of course it should happen before any part of your app actually needs some of the info. So, like mbehan suggested, you might want to perform that init check within didFinishLaunching of your appdelegate.
There's a method for that:
[[NSUserDefaults standardUserDefaults] registerDefaults:aDictionary];
The method takes a dictionary of keys/values for NSUserDefaults to use if the user hasn't set anything more specific.
You need to call this every time the app starts. Apple suggest:
The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.
I spent some time reviewing the iPhone documentation and multiple posts. I think the following is adequate for most purposes if you do not want to create settings for your app in the Settings application.
Here is a code snippet that you can drop in AppDelegate didFinishLaunching:
NSString *test = @"Test";
NSString *testKey = @"TestKey";
NSString *test2 = @"Test2";
NSString *test2Key = @"Test2Key";
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:test,testKey,test2,test2Key,nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];
/*
Alternative if we want to create default values in a file called Defaults.plist in Resources folder
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];
To set the preference within the app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"MyNewTest" forKey:@"TestKey"];
To retrieve the preference within the app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *results = [defaults stringForKey:@"TestKey"];
Can also use the following syntax:
intForKey:
floatForKey:
boolForKey:
objectForKey:
Returns Objective-C object like NSString, NDDate, or NSNumber
If the object is not a string, may need to specify the value, such as
Label.text = [[defaults objectForKey:@"TestPreferenceStoredAsNumber"]stringValue];
*/
Place the following in AppDelegate applicationWillTerminate and applicationDidEnterBackground:
[[NSUserDefaults standardUserDefaults]synchronize];
Then to view the preferences, place this code in a convenient spot:
// View all keys and values in Console
NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
精彩评论