1. Jun '09
    10

    Using plist for Default Preferences

    I was looking at the TVShow source and thought they did something very clever, they used a plist for default user preferences.
    This works great cause you can useĀ NSDictionary dictionaryWithContentsOfFile instead of having to construct a dictionary.

    Here is how its done.

    Make a new property list through File>New File…

    Name it something like “UserDefaults” and start adding values. These are the defaults for CopyPath.

    Now for the 3 lines of code to make it all work.

    NSString *userDefaultsPath = [[NSBundle mainBundle] pathForResource:@"UserDefaults" ofType:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:userDefaultsPath];
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

    First get the path to the plist in the bundle, then load up a dictionary with the contents of the file, lastly set the user defaults with the dictionary.

    Easy peasy lemon squeezy.