iPhone update application version (in Settings) [duplicate]

依然范特西╮ 提交于 2019-12-03 15:47:01

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'

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!