What's the most common scenario for Cocoa app setup during first launch?

一笑奈何 提交于 2019-12-01 01:29:16

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:.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!