I am creating an app and I would like a user to set some obligatory preferences during first app launch. What is the most common scenario to achieve this? Should I set some user defaults to see if the app has been setup? Also - if I determine that the app is being launched for the first time - how should I display "Setup" window? If I load it from the separte xib file - how will I deffer the display of main app window?
问题:
回答1:
The standard way to do this is in the +(void)initialize
method of your main controller class.
For example, in your interface (.h):
@interface MDAppController : NSObject {
BOOL MDFirstRun;
BOOL showInspector;
BOOL showIcons;
}
@end
Then in your .m file:
NSString * const MDFirstRunKey = @"MDFirstRun";
NSString * const MDShouldShowInspectorKey = @"MDShouldShowInspector";
NSString * const MDBrowserShouldShowIconsKey = @"MDBrowserShouldShowIcons";
@implementation
+ (void)initialize {
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:MDFirstRunKey];
[defaultValues setObject:[NSNumber numberWithBool:NO]
forKey:MDShouldShowInspectorKey];
[defaultValues setObject:[NSNumber numberWithBool:YES]
forKey:MDBrowserShouldShowIconsKey];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];
}
line break
- (id)init {
if (self = [super init]) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
MDFirstRun = [[userDefaults objectForKey:MDFirstRunKey] boolValue];
showInspector = [[userDefaults objectForKey:MDShouldShowInspectorKey] boolValue];
showIcons = [[userDefaults objectForKey:MDBrowserShouldShowIconsKey] boolValue];
}
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
if (MDFirstRun) {
[[NSUserDefaults standardUserDefaults]
setObject:[NSNumber numberWithBool:NO]
forKey:MDFirstRunKey];
// show first use panel
} else {
// do normal launch
}
}
/// other methods
@end
Basically, you set up all of your default values in your initialize method. (The initialize method is called very early on before init
is called, so it provides a convenient place to make sure user defaults all have default values). The registerDefaults:
method of NSUserDefaults
is special in that the values you pass in only are used if a particular value hasn't already been set. In other words, when in the code above, I set the first launch key to NO in the applicationDidFinishLaunching:
method, that overrides the default value and will be saved to your application's preferences plist file. The values that are saved in the preferences file take precedence over those that you've registered with user defaults in the initialize
method.
To defer opening of the main window, you basically want to make sure that the "Visible at Launch" flag is turned off for the window in question in the Attributes inspector in Interface Builder:
It's that flag that determines whether a window is shown as soon as the nib is loaded, or whether you will need to do it programmatically using something like makeKeyAndOrderFront:
.