iPhone更新的应用程序版本(在设置)
可能重复:
How can I display the application version revision in my application's settings bundle?iPhone更新的应用程序版本(在设置)
我有一个显示当前版本的设置iPhone应用程序不变(就像Skype的一样)。
当我发布应用程序的第一个版本,我用这个代码来设置应用程序设置:
- (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];
}
而且这个工作非常愉快。
我现在面临的问题是,如果您更新应用程序,这些默认值(设置)也不会重新编写,因此应用程序版本不会更新。
如何强制在每次安装时设置特定设置?
感谢 Gonso
你为什么不设置从mainBundle的版本号?
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
这样你就不必为每个版本更新设置文件。如果你想比较现有的和新的安装版本。您可以在启动时将版本号写入文件,并将目录版本与启动版本进行比较。
我有这样的代码在我的应用程序委托的-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.
但应用程序有前设置“版本”更新运行,以反映这一变化。
您可以使用下面的“运行脚本”构建阶段:
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
所有你需要做的,是你的应用程序的名称,以取代YourApp并设置相应的keyPath。
在我的情况,我有PreferenceSpecifiers数组中的4项,并且我需要设置从数组中最后一个项目的价值,这就是为什么我用“:PreferenceSpecifiers::默认值”
的情况下,你在你的应用程序名称 CFBundleShortVersionString ='在/ usr/libexec目录有空间/ PlistBuddy -c“打印:CFBundleShortVersionString”“$ {SRCROOT}/$ {INFOPLIST_FILE}”'“$ {SRCROOT}/$ {INFOPLIST_FILE}” /usr/libexec/PlistBuddy -c“Set:PreferenceSpecifiers:0:DefaultValue'$ {CFBundleShortVersionString }($ {CFBundleVersion})'“”$ {SRCROOT}/$ {PROJECT} /Settings.bundle/Root.plist“ – Zsolt 2013-05-14 11:23:09
但那么我不能在设置屏幕上有版本,我可以吗? 这是我如何定义版本: \t \t \t \t \t 类型 \t \t \t PSTitleValueSpecifier \t \t \t 标题 \t \t \t 版本 \t \t \t 重点 \t \t \t applicationVersion \t \t \t 默认值 \t \t \t 1.1.2 \t \t –
gonso
2009-08-24 15:52:36