【问题标题】:iPhone update application version (in Settings) [duplicate]iPhone更新应用程序版本(在设置中)[重复]
【发布时间】:2010-11-21 15:16:13
【问题描述】:

可能重复:
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];
}

这很好用。

我现在面临的问题是,如果你更新应用程序,这些默认值(设置)也不会重写,所以应用程序版本没有更新。

如何强制在每次安装时设置特定设置?

谢谢 贡索

【问题讨论】:

    标签: iphone settings


    【解决方案1】:

    您可以使用以下“运行脚本”构建阶段:

    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 替换为您的应用名称并设置适当的密钥路径。

    就我而言,我在 PreferenceSpecifiers 数组中有 4 项,我需要为数组中的最后一项设置值,这就是我使用 ':PreferenceSpecifiers:3:DefaultValue' 的原因

    【讨论】:

    • 如果您的应用名称中有空格 CFBundleShortVersionString=/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "${SRCROOT}/${INFOPLIST_FILE}" CFBundleVersion=/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "${SRCROOT}/${INFOPLIST_FILE}" /usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:0:DefaultValue '${CFBundleShortVersionString} ( ${CFBundleVersion})'" "${SRCROOT}/${PROJECT}/Settings.bundle/Root.plist"
    【解决方案2】:

    我的应用程序委托的 -applicationDidFinishLaunching 中有此代码:

    #if DDEBUG // debugging/testing
        NSString *versionString = [NSString stringWithFormat:@"v%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
    #else
        NSString *versionString = [NSString stringWithFormat:@"Version %@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
    #endif // DDEBUG
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setValue:versionString forKey:@"version"];
        printf("Version: = %s\n", [versionString cStringUsingEncoding:NSMacOSRomanStringEncoding]);
        [defaults synchronize]; // force immediate saving of defaults.
    

    但应用程序必须在设置“版本”更新之前运行以反映更改。

    【讨论】:

      【解决方案3】:

      为什么不从 mainBundle 中设置版本号?

      NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
      

      这样您就不必为每个版本更新设置文件。如果要比较现有安装版本与新安装版本。您可以在启动时将版本号写入文件,并将目录版本与启动版本进行比较。

      【讨论】:

      • 但是我不能在“设置”屏幕中看到该版本,可以吗?这就是我定义版本的方式: TypePSTitleValueSpecifierTitleVersionKeyapplicationVersionDefaultValue1.1.2
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2013-12-18
      相关资源
      最近更新 更多