在编译时更新 Root.Plist [英] Update Root.Plist on Compile

查看:46
本文介绍了在编译时更新 Root.Plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用设置文件,其中显示了我们应用的版本号.这个想法是在编译时更新 Root.plist,这样我们就不必更新两个地方而不是一个地方.我们更新了构建设置版本,并希望这些设置在编译时更新 Root.plist.

I have an app settings file which displays the version number for our app. The idea is to update the Root.plist on compile so that we don't have to update two places rather than one. We update theBuild settings version and would like those settings to update the Root.plist on compile.

此代码提取构建设置的信息,如何更新 Root.plist 文件?

This code extracts the information for the build settings, how do I update the Root.plist file?

NSString *appBuildNo = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *appBuildVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
NSString *appBuildStr = [NSString stringWithFormat:@"%@ Build %@", appBuildNo, appBuildVersion];

NSURL * settingsURL =  [[NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"Settings" withExtension:@"bundle"]]
                        URLForResource:@"Root" withExtension:@"plist"];
NSDictionary * settingsDict = [NSDictionary dictionaryWithContentsOfURL:settingsURL];
NSArray * settingsArr = [settingsDict objectForKey:@"PreferenceSpecifiers"];
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
for( NSDictionary * setting in settingsArr ){
    NSString * key = [setting objectForKey:@"DefaultValue"];
    [defaults setValue:appBuildStr forKey:key];
    [defaults synchronize];
    break;
}

这个想法是默认更新 Root.plist 但它不起作用.任何帮助将不胜感激

The idea is that defaults updates Root.plist but it is not working. Any help would be appreciated

推荐答案

Droppy 在帮助我解决这个问题方面非常有帮助.设置好设置包和 Root.plist 文件后,我将以下代码添加到我的 AppDelegate 并从 didFinishLaunchingWithOptions 中调用它.

Droppy was very helpful in helping me to fix this. After setting up my Settings Bundle and the Root.plist file, I added the following code to my AppDelegate and called it from the didFinishLaunchingWithOptions.

- (void)updateVersionInfo
{
   //This method updates the Root settings to display current Version and Build No in Settings Bundle
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSString *appVersionNumber = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
   NSString *appBuildNo = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

   NSString *versionNumberInSettings = [NSString stringWithFormat:@"%@ Build %@", appBuildNo, appVersionNumber];

   [defaults setObject:versionNumberInSettings forKey:@"version"];
}

此代码然后将 info.plist 文件中版本的内容添加到我的设置包中.

This code then adds the contents of the Version in the info.plist file to my Settings Bundle.

这篇关于在编译时更新 Root.Plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆