如何在用户打开应用程序之前更改应用程序的设置包设置(仅在存档后进行)

时间:2014-01-30 20:35:38

标签: xcode

这是我的问题。在我打开应用程序之前将我的应用程序存档并安装到我的Iphone之后,我检查设置并看到设置包没有更新。它只显示默认输入(如下图所示)。

enter image description here

我希望它显示更新的设置(如下所示)。

enter image description here

这是我的AppDelegate.m中的代码

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // refresh upload queue
    _uploadQueue = [[SCUploadQueue alloc] init];
    [_uploadQueue refreshUpload];

    return YES;
}

-(void) updateVersionInfo{
    // Get Settings.bundle object
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // Get values from .plist file generated by build phase script
    NSString *versionNumber = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];


    // Dealing with the date
    NSString *dateFromSettings = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];

    // Create the version number
    NSString *versionNumberInSettings = [NSString stringWithFormat:@"%@", versionNumber];
    NSLog(@"Version: %@", versionNumberInSettings);
    NSLog(@"Build date: %@", dateFromSettings);//buildDate);

    // Set the build date and version number in the settings bundle reflected in app settings.
    [defaults setObject:versionNumberInSettings forKey:@"version"];
    [defaults setObject:dateFromSettings forKey:@"buildDate"];

}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // refresh upload queue
    _uploadQueue = [[SCUploadQueue alloc] init];
    [_uploadQueue refreshUpload];

    return YES;
}

-(void) updateVersionInfo{
    // Get Settings.bundle object
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // Get values from .plist file generated by build phase script
    NSString *versionNumber = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CFBundleShortVersionString"];


    // Dealing with the date
    NSString *dateFromSettings = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBuildDate"];

    // Create the version number
    NSString *versionNumberInSettings = [NSString stringWithFormat:@"%@", versionNumber];
    NSLog(@"Version: %@", versionNumberInSettings);
    NSLog(@"Build date: %@", dateFromSettings);//buildDate);

    // Set the build date and version number in the settings bundle reflected in app settings.
        [defaults setObject:versionNumberInSettings forKey:@"version"];
        [defaults setObject:dateFromSettings forKey:@"buildDate"];

    }

当我从xcode构建它时,设置有效。但它不适用于归档。如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:3)

您可以使用构建阶段运行脚本来完成此任务。

添加运行脚本:

iOS Developer Doc

  1. 在项目编辑器中,选择要添加的目标 运行脚本构建阶段。
  2. 单击项目编辑器顶部的Build Phases。
  3. 选择编辑器>添加构建阶段>添加运行脚本构建阶段。
  4. Editor > Add Build Phase

    在“运行脚本”模板中配置脚本

    。在shell输入框的正下方是您添加脚本的地方。

    run script

    示例脚本

    您可以在脚本中使用PlistBuddy。下面是一个示例脚本,它利用PlistBuddy从应用程序主Info.plist中提取CFBundleShortVersionString和CFBundleVersion,并将它们作为默认值添加到Settings bundle Root.plist中。

    注意:在此示例脚本中,项目Root.plist的路径可能不同。此外,需要更改PreferenceSpecifier的索引以匹配Root.plist文件的设置。

    version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
    build=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
    
    /usr/libexec/PlistBuddy "$SRCROOT/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:1:DefaultValue $version"
    /usr/libexec/PlistBuddy "$SRCROOT/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:2:DefaultValue $build"