可能重复:
如何在应用程序的设置包中显示应用程序版本修订版?
我有一个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];
}
Run Code Online (Sandbox Code Playgroud)
这工作得很好,花花公子.
我现在面临的问题是,如果更新应用程序,则不会重写这些默认值(设置),因此不会更新应用程序版本.
如何强制每次安装时都设置特定的设置?
谢谢Gonso
您可以使用以下"运行脚本"构建阶段:
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
Run Code Online (Sandbox Code Playgroud)
您需要做的就是用您的应用程序名称替换YourApp并设置适当的密钥路径.
在我的例子中,我在PreferenceSpecifiers数组中有4个项目,我需要设置数组中最后一项的值,这就是我使用':PreferenceSpecifiers:3:DefaultValue'的原因
为什么不从 mainBundle 设置版本号?
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
Run Code Online (Sandbox Code Playgroud)
这样您就不必为每个版本更新设置文件。如果您想比较现有版本与新安装版本。您可以在启动时将版本号写入文件,并将目录版本与启动版本进行比较。
| 归档时间: |
|
| 查看次数: |
5643 次 |
| 最近记录: |