views:

390

answers:

2

Hello

I have an iPhone application that displays the current version as a Settings constant (just like Skype does).

When I released the first version of the application, I use this code to set the app Settings:

- (void)registerDefaultsFromSettingsBundle {
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle) {
        NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];

    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences) {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key) {
      [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];

        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

And this worked fine and dandy.

The problem I face now is that if you update the application, these defaults (Settings) are nor re-written, so the application version is not updated.

How can I force that an specific Settings is set on every install?

Thanks Gonso

A: 

Why wouldn't you set the version number from the mainBundle?

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];

This way you don't have to update the settings file for every version. If you want to compare existing versus new install version. You could write out the version number to a file on launch and compare the directory version with the launch version.

Jordan
But then I cant have the version in the Settings screen, can I? This is how I define the Version: <dict> <key>Type</key> <string>PSTitleValueSpecifier</string> <key>Title</key> <string>Version</string> <key>Key</key> <string>applicationVersion</string> <key>DefaultValue</key> <string>1.1.2</string> </dict>
gonso
A: 

This question has a number of useful answers to your question:

http://stackoverflow.com/questions/877128/how-can-i-display-the-application-version-revision-in-my-applications-settings-b

JosephH