iPhone update application version (in Settings) [d

2019-03-29 07:19发布

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

3条回答
唯我独甜
2楼-- · 2019-03-29 07:37

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'

查看更多
虎瘦雄心在
3楼-- · 2019-03-29 07:55

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.

查看更多
乱世女痞
4楼-- · 2019-03-29 07:58

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.

查看更多
登录 后发表回答