5

Possible Duplicate:
How can I display the application version revision in my application's settings bundle?

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

Community
  • 1
  • 1
gonso
  • 2,065
  • 5
  • 27
  • 35

3 Answers3

7

You can use the following 'Run Script' Build Phase:

CFBundleShortVersionString=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" ${SRCROOT}/YourApp/YourApp-Info.plist`
CFBundleVersion=`/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" ${SRCROOT}/YourApp/YourApp-Info.plist`
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:3:DefaultValue '${CFBundleShortVersionString} (${CFBundleVersion})'" ${SRCROOT}/YourApp/Settings.bundle/Root.plist

All you need to do, is to replace YourApp with your app's name and set the appropriate keypath.

In my case, I have 4 items in the PreferenceSpecifiers array, and I need to set the value for the last item from the array and that's why I used ':PreferenceSpecifiers:3:DefaultValue'

arturgrigor
  • 9,361
  • 4
  • 31
  • 31
  • 1
    in case you have a space in your app name CFBundleShortVersionString=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${SRCROOT}/${INFOPLIST_FILE}"` CFBundleVersion=`/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "${SRCROOT}/${INFOPLIST_FILE}"` /usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:0:DefaultValue '${CFBundleShortVersionString} (${CFBundleVersion})'" "${SRCROOT}/${PROJECT}/Settings.bundle/Root.plist" – Zsolt May 14 '13 at 11:23
1

I have this code in my application delegate's -applicationDidFinishLaunching:

#if DDEBUG // debugging/testing
    NSString *versionString = [NSString stringWithFormat:@"v%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
#else
    NSString *versionString = [NSString stringWithFormat:@"Version %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
#endif // DDEBUG
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:versionString forKey:@"version"];
    printf("Version: = %s\n", [versionString cStringUsingEncoding:NSMacOSRomanStringEncoding]);
    [defaults synchronize]; // force immediate saving of defaults.

But app has to be run before Settings 'version' updates to reflect the change.

westsider
  • 4,967
  • 5
  • 36
  • 51
0

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
  • 21,746
  • 10
  • 51
  • 63
  • But then I cant have the version in the Settings screen, can I? This is how I define the Version: Type PSTitleValueSpecifier Title Version Key applicationVersion DefaultValue 1.1.2 – gonso Aug 24 '09 at 15:52