【问题标题】:How can I display the application version revision in my application's settings bundle?如何在我的应用程序设置包中显示应用程序版本修订?
【发布时间】:2010-10-27 00:02:41
【问题描述】:

我想在我的应用程序设置包中包含应用程序版本和内部修订,例如 1.0.1 (r1243)。

Root.plist 文件包含这样的片段...

     <dict>
        <key>Type</key>
        <string>PSTitleValueSpecifier</string>
        <key>Title</key>
        <string>Version</string>
        <key>Key</key>
        <string>version_preference</string>
        <key>DefaultValue</key>
        <string>VersionValue</string>
        <key>Values</key>
        <array>
            <string>VersionValue</string>
        </array>
        <key>Titles</key>
        <array>
            <string>VersionValue</string>
        </array>
    </dict>

我想在构建时替换“VersionValue”字符串。

我有一个可以从我的存储库中提取版本号的脚本,我需要的是一种在构建时处理(预处理)Root.plist 文件并替换修订号而不影响源文件的方法.

【问题讨论】:

    标签: iphone xcode application-settings settings-bundle


    【解决方案1】:

    我相信您可以使用类似于我在this answer 中描述的方式(基于this post)来做到这一点。

    首先,您可以通过将 VersionValue 重命名为 ${VERSIONVALUE} 来使 VersionValue 成为 Xcode 中的变量。创建一个名为 versionvalue.xcconfig 的文件并将其添加到您的项目中。转到您的应用程序目标并转到该目标的构建设置。我相信您需要将 VERSIONVALUE 添加为用户定义的构建设置。在该窗口的右下角,将“基于”值更改为“版本值”。

    最后,转到您的目标并创建一个运行脚本构建阶段。检查运行脚本阶段并将脚本粘贴到脚本文本字段中。例如,我用当前 Subversion 构建标记我的 BUILD_NUMBER 设置的脚本如下:

    REV=`/usr/bin/svnversion -nc ${PROJECT_DIR} | /usr/bin/sed -e 's/^[^:]*://;s/[A-Za-z]//'`
    echo "BUILD_NUMBER = $REV" > ${PROJECT_DIR}/buildnumber.xcconfig
    

    当这些值在您的项目中发生变化时,这应该可以替换变量。

    【讨论】:

    • 如果我想将版本号嵌入到 Info.plist 文件中,这可以工作。但我不能让它适用于其他 plist 文件,例如位于 Settings.bundle 中的 Root.plist 文件。我可以使用构建设置来启用它吗?
    【解决方案2】:

    我设法通过使用 pListcompiler (http://sourceforge.net/projects/plistcompiler) 开源项目来做我想做的事。

    1. 使用此编译器,您可以使用以下格式将属性文件写入 .plc 文件:

      plist {
          dictionary {
              key "StringsTable" value string "Root"
              key "PreferenceSpecifiers" value array [
                  dictionary {
                      key "Type" value string "PSGroupSpecifier"
                      key "Title" value string "AboutSection"
                  }
                  dictionary {
                      key "Type" value string "PSTitleValueSpecifier"
                      key "Title" value string "Version"
                      key "Key" value string "version"
                      key "DefaultValue" value string "VersionValue"
                      key "Values" value array [
                          string "VersionValue"
                      ]
                      key "Titles" value array [
                          string "r" kRevisionNumber
                      ]
                  }
              ]
          }
      }
      
    2. 我有一个自定义运行脚本构建阶段,该阶段将我的存储库修订提取到 .h 文件,如 brad-larson here 所述。

    3. plc 文件可以包含预处理器指令,如#define、#message、#if、#elif、#include、#warning、#ifdef、#else、#pragma、#error、#ifndef、#endif、xcode 环境变量。所以我可以通过添加以下指令来引用变量 kRevisionNumber

      #include "Revision.h"
      
    4. 我还在我的 xcode 目标中添加了一个自定义脚本构建阶段,以便在每次构建项目时运行 plcompiler

      /usr/local/plistcompiler0.6/plcompile -dest Settings.bundle -o Root.plist Settings.plc
      

    就是这样!

    【讨论】:

    • 这听起来像很多工作只是为了替换 plist 文件中的单个值...在构建 plist 时能够访问变量在概念上很酷,但使用工具更容易为 plist 文件构建。我在回答中描述了 PlistBuddy — 试一试!
    【解决方案3】:

    还有另一种解决方案,它可能比之前的任何一个答案都简单得多。 Apple 在其大多数安装程序中捆绑了一个名为 PlistBuddy 的命令行工具,并将其包含在 Leopard 中,地址为 /usr/libexec/PlistBuddy

    由于您要替换VersionValue,假设您已将版本值提取到$newVersion,您可以使用以下命令:

    /usr/libexec/PlistBuddy -c "Set :VersionValue $newVersion" /path/to/Root.plist
    

    无需摆弄 sed 或正则表达式,这种方法非常简单。有关详细说明,请参阅man page。您可以使用 PlistBuddy 添加、删除或修改属性列表中的任何条目。例如,我的一个朋友使用 PlistBuddy 在博客中提到了 incrementing build numbers in Xcode

    注意:如果您只提供 plist 的路径,PlistBuddy 会进入交互模式,因此您可以在决定保存更改之前发出多个命令。我绝对建议在将其放入构建脚本之前执行此操作。

    【讨论】:

    • 我花了一段时间才弄清楚在我的 plist 中引用版本号的正确方法;就我而言,结果是 /usr/libexec/PlistBuddy Settings.bundle/Root.plist -c "set PreferenceSpecifiers:0:DefaultValue $newversion" - 希望这对其他人有用。
    • Quinn Taylor, JosephH,感谢您的回答,我能够在 Settings.bundle 中自动实现我的应用程序版本号。为你们俩+1 ;-)
    • 从自定义“运行脚本”构建阶段,我需要包含更多到 Root.plist 的路径:/usr/libexec/PlistBuddy ${TARGET_BUILD_DIR}/${FULL_PRODUCT_NAME}/Settings.bundle /Root.plist -c "set PreferenceSpecifiers:0:DefaultValue $newVersion"
    • 为了完整起见,这是另一种对我有用的 PListBuddy 方法:xcodehelp.blogspot.com/2012/05/…
    • 最正确的方式是/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:0:DefaultValue ${newVersion}" "${TARGET_BUILD_DIR}/${CONTENTS_FOLDER_PATH}/Settings.bundle/Root.plist"
    【解决方案4】:

    我懒人的解决方案是从我的应用程序代码中更新版本号。您可以在 Root.plist 中有一个默认(或空白)值,然后在您的启动代码中的某个位置:

    NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
    [[NSUserDefaults standardUserDefaults] setObject:version forKey:@"version_preference"];
    

    唯一需要注意的是,您的应用必须至少运行一次,更新版本才会出现在设置面板中。

    您可以进一步了解这个想法并更新,例如,您的应用启动次数的计数器,或其他有趣的信息。

    【讨论】:

    • 这将起作用,除非用户在启动您的应用之前进入设置。
    • @Moshe 是的,但要优雅地处理这个问题,您可以简单地在 .plist 文件中指定一个默认值,可能类似于“尚未启动”
    • 虽然大多数开发人员可能将CFBundleShortVersionStringCFBundleVersion 设置为相同的值,但CFBundleShortVersionString 实际上是Apple wants you to consider your released version,这将是您向用户显示的内容。 CFBundleVersion 可能是内部版本号,您可能不应该向用户显示(如果不同)。
    • 我错过了什么吗?这正是我正在做的,但价值没有改变。你们没有使用我认为是只读的 Title 属性吗?
    • 更新应用时还有一个问题。在至少启动一次更新的应用程序之前,设置包仍会显示旧的构建版本。
    【解决方案5】:

    基于示例here,这是我用来自动更新设置包版本号的脚本:

    #! /usr/bin/env python
    import os
    from AppKit import NSMutableDictionary
    
    settings_file_path = 'Settings.bundle/Root.plist' # the relative path from the project folder to your settings bundle
    settings_key = 'version_preference' # the key of your settings version
    
    # these are used for testing only
    info_path = '/Users/mrwalker/developer/My_App/Info.plist'
    settings_path = '/Users/mrwalker/developer/My_App/Settings.bundle/Root.plist'
    
    # these environment variables are set in the XCode build phase
    if 'PRODUCT_SETTINGS_PATH' in os.environ.keys():
        info_path = os.environ.get('PRODUCT_SETTINGS_PATH')
    
    if 'PROJECT_DIR' in os.environ.keys():
        settings_path = os.path.join(os.environ.get('PROJECT_DIR'), settings_file_path)
    
    # reading info.plist file
    project_plist = NSMutableDictionary.dictionaryWithContentsOfFile_(info_path)
    project_bundle_version = project_plist['CFBundleVersion']
    
    # print 'project_bundle_version: '+project_bundle_version
    
    # reading settings plist
    settings_plist = NSMutableDictionary.dictionaryWithContentsOfFile_(settings_path)
      for dictionary in settings_plist['PreferenceSpecifiers']:
        if 'Key' in dictionary and dictionary['Key'] == settings_key:
            dictionary['DefaultValue'] = project_bundle_version
    
    # print repr(settings_plist)
    settings_plist.writeToFile_atomically_(settings_path, True)
    

    这是我在 Settings.bundle 中的 Root.plist:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>PreferenceSpecifiers</key>
        <array>
            <dict>
                <key>Title</key>
                <string>About</string>
                <key>Type</key>
                <string>PSGroupSpecifier</string>
            </dict>
            <dict>
                <key>DefaultValue</key>
                <string>1.0.0.0</string>
                <key>Key</key>
                <string>version_preference</string>
                <key>Title</key>
                <string>Version</string>
                <key>Type</key>
                <string>PSTitleValueSpecifier</string>
            </dict>
        </array>
        <key>StringsTable</key>
        <string>Root</string>
    </dict>
    </plist>
    

    【讨论】:

    • 非常有用 - 我在从 Python 执行 PlistBuddy 时遇到了麻烦,而且我从没想过使用 NSDictionary(也没有意识到它可以让您如此轻松地访问 plist 文件)
    • 谢谢你。一项修改——正如你现在所拥有的,它对源代码进行了更改,而不是 builddir——这意味着你在设备或模拟器中看到的内容将始终是实际构建版本之后的一个构建版本。为了解决这个问题,我修改了您的脚本以首先迭代源代码,然后是 builddir,即。 settings_path_build = os.path.join(os.environ.get('TARGET_BUILD_DIR'), settings_file_path_build)
    • ... 而且,我附加了 githash:gitHash = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"]).rstrip()
    【解决方案6】:

    根据@Quinn 的回答,这里是我用来执行此操作的完整流程和工作代码。

    • 将设置包添加到您的应用程序。不要重命名。
    • 在文本编辑器中打开 Settings.bundle/Root.plist

    将内容替换为:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"     "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>PreferenceSpecifiers</key>
        <array>
            <dict>
                <key>Title</key>
                <string>About</string>
                <key>Type</key>
                <string>PSGroupSpecifier</string>
            </dict>
            <dict>
                <key>DefaultValue</key>
                <string>DummyVersion</string>
                <key>Key</key>
                <string>version_preference</string>
                <key>Title</key>
                <string>Version</string>
                <key>Type</key>
                <string>PSTitleValueSpecifier</string>
            </dict>
        </array>
        <key>StringsTable</key>
        <string>Root</string>
    </dict>
    </plist>
    
    • 创建一个运行脚本构建阶段,移到复制捆绑资源阶段之后。添加此代码:

      cd "${BUILT_PRODUCTS_DIR}"
      buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_PATH}" )
      /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:1:DefaultValue $buildVersion" "${WRAPPER_NAME}/Settings.bundle/Root.plist"
      
    • 将 MyAppName 替换为您的实际应用名称,并将 PreferenceSpecifiers 后面的 1 替换为设置中您的版本条目的索引。上面的 Root.plist 示例的索引为 1。

    【讨论】:

    • 我认为这是最好的方法
    • 我试过这个,我看到我的设置包中的标题值发生了变化。标题出现在 InAppSettingsKit 中,但值与初始版本相比没有变化。标题永远不会出现在“设置”应用中。我放弃了,当用户在菜单中选择“关于”时,我将弹出一个对话框'
    • 使用此方法时,设置不是只读的。即我可以在 settings.app 中点击版本号设置,它是可编辑的。
    • bash 脚本 @ben-clayton put 对我不起作用,所以我根据他的回答重新制作它,这里是:buildVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")/usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:3:DefaultValue $buildVersion" "${SRCROOT}/Settings.bundle/Root.plist"
    • 您可以使用 ${INFOPLIST_PATH} 作为 info plist 路径
    【解决方案7】:

    由于一个原因,其他答案无法正常工作: 在打包设置捆绑包之后,才会执行运行脚本构建阶段。因此,如果您的 Info.plist 版本是 2.0.11,并且您将其更新为 2.0.12,然后构建/归档您的项目,设置包仍会显示 2.0.11。如果您打开 Settings bundle Root.plist,您可以看到版本号在构建过程结束之前不会更新。您可以再次构建项目以正确更新设置包,或者您可以将脚本添加到预构建阶段...

    • 在 XCode 中,为您的项目目标编辑方案
    • 单击 BUILD 方案上的披露箭头
    • 然后,点击“Pre-actions”项
    • 单击加号并选择“新建运行脚本操作”
    • 将 shell 值设置为 /bin/sh
    • 将“提供构建设置来自”设置为您的项目目标
    • 将您的脚本添加到文本区域。以下脚本对我有用。您可能需要修改路径以匹配您的项目设置:

      versionString=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")

      /usr/libexec/PlistBuddy "$SRCROOT/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:0:DefaultValue $versionString"

    这将在构建/归档过程中打包设置包之前正确运行脚本。如果您打开 Settings bundle Root.plist 并构建/归档您的项目,您现在将看到在构建过程开始时更新了版本号,并且您的 Settings bundle 将显示正确的版本。

    【讨论】:

    • 谢谢,只有您的解决方案显示正确的构建版本。需要构建两次的其他解决方案。
    • 这仍然需要我使用 Xcode 10.0 进行第二次构建
    • @Patrick iOS 设置应用程序有时会保留旧信息。要查看更改,您必须关闭并重新启动“设置”应用。
    • 顺便说一句,我找到了一种添加此脚本的更简单方法:转到项目目标的“构建阶段”选项卡,然后单击“+”图标。选择“New Run Script Phase”并在其中添加脚本代码。这是关键:单击并将新的运行脚本拖到 Build Phases 列表的顶部,在 Target Dependencies 下,但在 Compile Sources 之前。这将与预构建脚本的行为相同,并且更容易找到。
    • 感谢@Andy,您添加到“构建阶段”选项卡的解决方案效果很好。
    【解决方案8】:

    使用 Ben Clayton 的 plist https://stackoverflow.com/a/12842530/338986

    Copy Bundle Resources 之后添加Run script 和以下sn-p。

    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 -c "Set PreferenceSpecifiers:1:DefaultValue $version ($build)" "$CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist"
    

    CFBundleShortVersionString 之外附加CFBundleVersion。 它发出这样的版本:

    写信给 $CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist 而不是$SRCROOT 中的那个有一些好处。

    1. 它不会修改存储库工作副本中的文件。
    2. 您不需要在$SRCROOT 中区分Settings.bundle 的路径。路径可能会有所不同。

    在 Xcode 7.3.1 上测试

    【讨论】:

    • 如果您将脚本添加到项目方案的构建、预操作部分,这是 IMO 的最佳答案。看看安迪的回答。
    • 这对我有用。请记住将“DefaultValue”更改为特定于您。例如,我想更改页脚,所以我使用了“FooterText”。您还需要更改“PreferenceSpecifiers”之后的数字,使其与 plist 中的项目相关。
    【解决方案9】:

    我的工作示例基于 @Ben Clayton 的回答以及 @Luis Ascorbe 和 @Vahid Amiri 的 cmets:

    注意:此方法会修改存储库工作副本中的 Settings.bundle/Root.plist 文件

    1. 将设置包添加到您的项目根目录。不要重命名

    2. 打开 Settings.bundle/Root.plist 作为源代码

      将内容替换为:

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
      <plist version="1.0">
      <dict>
          <key>PreferenceSpecifiers</key>
          <array>
              <dict>
                  <key>DefaultValue</key>
                  <string></string>
                  <key>Key</key>
                  <string>version_preference</string>
                  <key>Title</key>
                  <string>Version</string>
                  <key>Type</key>
                  <string>PSTitleValueSpecifier</string>
              </dict>
          </array>
          <key>StringsTable</key>
          <string>Root</string>
      </dict>
      </plist>
      
    3. 将以下脚本添加到项目(目标)方案的 Build、Pre-actions 部分

      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 -c "Set PreferenceSpecifiers:0:DefaultValue $version ($build)" "${SRCROOT}/Settings.bundle/Root.plist"
      
    4. 构建并运行当前方案

    【讨论】:

      【解决方案10】:

      以上答案对我不起作用,因此我创建了自定义脚本。

      这会动态更新来自 Root.plist 的条目

      使用下面的运行脚本。 W 肯定在 xcode 10.3 中验证过。

      “var buildVersion”是标题中要显示的版本。

      在settings.bundle Root.plist中,标识符名称是下面的“var version”

      cd "${BUILT_PRODUCTS_DIR}"
      
      #set version name to your title identifier's string from settings.bundle
      var version = "Version"
      
      #this will be the text displayed in title
      longVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_PATH}")
      shortVersion=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" ${TARGET_BUILD_DIR}/${INFOPLIST_PATH})
      buildVersion="$shortVersion.$longVersion"
      
      path="${WRAPPER_NAME}/Settings.bundle/Root.plist"
      
      settingsCnt=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:" ${path} | grep "Dict"|wc -l`
      
      for (( idx=0; idx<$settingsCnt; idx++ ))
      do
      #echo "Welcome $idx times"
      val=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:Key" ${path}`
      #echo $val
      
      #if ( "$val" == "Version" )
      if [ $val == "Version" ]
      then
      #echo "the index of the entry whose 'Key' is 'version' is $idx."
      
      # now set it
      /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:${idx}:DefaultValue $buildVersion" $path
      
      # just to be sure that it worked
      ver=`/usr/libexec/PlistBuddy -c "Print PreferenceSpecifiers:${idx}:DefaultValue" $path`
      #echo 'PreferenceSpecifiers:$idx:DefaultValue set to: ' $ver
      
      fi
      
      done
      

      Root.plist 中的示例条目

          <dict>
              <key>Type</key>
              <string>PSTitleValueSpecifier</string>
              <key>Title</key>
              <string>Version</string>
              <key>DefaultValue</key>
              <string>We Rock</string>
              <key>Key</key>
              <string>Version</string>
          </dict>
      

      【讨论】:

        【解决方案11】:

        使用 Xcode 11.4,您可以使用以下步骤在应用程序的设置包中显示应用程序版本。


        设置$(MARKETING_VERSION)$(CURRENT_PROJECT_VERSION)变量

        注意:如果 Info.plist 中的 Bundle version string (short)Bundle version 键出现 $(MARKETING_VERSION)$(CURRENT_PROJECT_VERSION) 变量,您可以跳过以下步骤并跳转到下一部分。

        1. 打开 Xcode 项目。
        2. 打开 Project Navigator (cmd1),选择您的项目以显示您的项目设置,然后选择应用目标。
        3. 选择常规标签。
        4. Identity 部分,将 Version 字段内容更改为某个新值(例如 0.1.0)并更改 Build 字段内容到某个新值(例如12)。这 2 项更改将在 Info.plist 文件中创建 $(MARKETING_VERSION)$(CURRENT_PROJECT_VERSION) 变量。

        创建和配置设置包

        1. Project Navigator 中,选择您的项目。
        2. 选择文件 > 新建 > 文件...cmdN)。
        3. 选择iOS标签。
        4. 资源部分中选择Settings Bundle,然后点击下一步创建
        5. 选择 Root.plist 并将其作为源代码打开。将其内容替换为以下代码:
        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
        <plist version="1.0">
        <dict>
            <key>PreferenceSpecifiers</key>
            <array>
                <dict>
                    <key>DefaultValue</key>
                    <string></string>
                    <key>Key</key>
                    <string>version_preference</string>
                    <key>Title</key>
                    <string>Version</string>
                    <key>Type</key>
                    <string>PSTitleValueSpecifier</string>
                </dict>
            </array>
            <key>StringsTable</key>
            <string>Root</string>
        </dict>
        </plist>
        

        添加运行脚本

        1. Project Navigator 中,选择您的项目。
        2. 选择应用目标。
        3. 选择构建阶段标签。
        4. 点击+ > 新建运行脚本阶段
        5. 将新阶段拖放到复制捆绑资源部分上方的某处。这样,脚本将在编译应用程序之前执行。
        6. 打开新添加的运行脚本阶段,添加如下脚本:
        version="$MARKETING_VERSION"
        build="$CURRENT_PROJECT_VERSION"
        /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:0:DefaultValue $version ($build)" "${SRCROOT}/Settings.bundle/Root.plist"
        

        启动应用

        1. 在设备或模拟器上运行产品 (cmdR)。
        2. 在设备或模拟器上,应用启动后,打开设置应用并在第三方应用列表中选择您的应用。应用的版本应如下所示:


        来源

        【讨论】:

        • 这对我来说是一个错误Set: Entry, "PreferenceSpecifiers:0:DefaultValue", Does Not Exist
        • 这对我有用:/usr/libexec/PlistBuddy "$SRCROOT/AppName/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:0:DefaultValue $version"
        • 谢谢。这对我来说是什么世界。但我的被命名为 Settings-Watch.bundle 并删除了($build)
        • 太棒了!对我来说,这里的关键帮助是使用@Ben Clayton 的答案,但修改运行脚本以使用 $MARKETING_VERSION 和 $CURRENT_PROJECT_VERSION 正如您所指出的那样。这对我来说是必要的,因为这些版本号现在实际上并没有直接存储在 Info.plist 中,因此在这种情况下,在运行脚本中读取 Info.plist 并没有帮助(这是 Xcode 现在的默认设置)。
        • 哦,还有一些小细节,但是您的运行脚本不必要地重新定义了$MARKETING_VERSION -> $version - 您可以直接将$MARKETING_VERSION 放在 PlistBuddy 命令中,使其成为单行代码。
        【解决方案12】:

        对我来说这是最简单的解决方案:

        在复制捆绑资源步骤之前添加新的脚本构建阶段

        壳牌:/usr/bin/env python

        内容:

        #! /usr/bin/env python
        import os
        from AppKit import NSMutableDictionary
        
        # Key to replace
        settings_key = 'version_preference' # the key of your settings version
        
        # File path
        settings_path = os.environ.get('SRCROOT') + "/TheBeautifulNameOfYourOwnApp/Settings.bundle/Root.plist"
        
        # Composing version string
        version_string = os.environ.get('MARKETING_VERSION') + " (" + os.environ.get('CURRENT_PROJECT_VERSION') + ")"
        
        # Reading settings plist
        settings_plist = NSMutableDictionary.dictionaryWithContentsOfFile_(settings_path)
        for dictionary in settings_plist['PreferenceSpecifiers']:
            if 'Key' in dictionary and dictionary['Key'] == settings_key:
                dictionary['DefaultValue'] = version_string
        
        # Save new settings
        settings_plist.writeToFile_atomically_(settings_path, True)
        

        【讨论】:

          【解决方案13】:

          这些是我在 Xcode 12.2 的 swift 项目中必须使用的变量

          version=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$PROJECT_DIR/$INFOPLIST_FILE")
          build="$CURRENT_PROJECT_VERSION"
          
          /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:0:FooterText Version $version" "$CODESIGNING_FOLDER_PATH/Settings.bundle/ServerURLSettings.plist"
          /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:0:FooterText Version $version($build)" "$CODESIGNING_FOLDER_PATH/Settings.bundle/DeveloperSettings.plist"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-14
            • 1970-01-01
            • 1970-01-01
            • 2013-01-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-06-28
            相关资源
            最近更新 更多