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

后端 未结 1 537
执笔经年
执笔经年 2021-01-07 05:47

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

相关标签:
1条回答
  • 2021-01-07 06:27

    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:

    alt text

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

    0 讨论(0)
提交回复
热议问题