Possible Duplicate:
How can I display the application version revision in my application's settings bundle?
我有一个 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];
}
效果很好。
我现在面临的问题是,如果更新应用程序,这些默认值(设置)也不会被重写,因此应用程序版本不会更新。
如何强制在每次安装时设置特定设置?
谢谢 贡索
最佳答案
您可以使用以下“运行脚本”构建阶段:
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 替换为您的应用程序名称并设置适当的 key 路径。
就我而言,PreferenceSpecifiers 数组中有 4 个项目,我需要设置数组中最后一个项目的值,这就是我使用 ':PreferenceSpecifiers:3:DefaultValue' 的原因
关于iPhone 更新应用程序版本(在“设置”中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1311405/