在编译时更新Root.Plist

时间:2014-09-25 14:25:28

标签: ios xcode plist nsuserdefaults

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

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

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但它不起作用。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

Droppy非常有助于我解决这个问题。在设置我的设置包和Root.plist文件之后,我将以下代码添加到我的AppDelegate并从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文件中的Version内容添加到我的设置包中。