How can I display the application version revision in my application's settings bundle?

前端 未结 13 718
一个人的身影
一个人的身影 2020-11-29 15:20

I would like to include the application version and internal revision, something like 1.0.1 (r1243), in my application\'s settings bundle.

The Root.plist file contai

相关标签:
13条回答
  • 2020-11-29 15:49

    My working Example based on @Ben Clayton answer and the comments of @Luis Ascorbe and @Vahid Amiri:

    Note: This approach modifies the Settings.bundle/Root.plist file in working copy of repository

    1. Add a settings bundle to your project root. Don't rename it

    2. Open Settings.bundle/Root.plist as SourceCode

      Replace the contents with:

      <?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. Add the following script to the Build, Pre-actions section of the project (target) scheme

      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. Build and Run the current scheme

    0 讨论(0)
  • 2020-11-29 15:50

    Based on @Quinn's answer, here the full process and working code I use to do this.

    • Add a settings bundle to your app. Don't rename it.
    • Open Settings.bundle/Root.plist in a text editor

    Replace the contents with:

    <?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>
    
    • Create a Run Script build phase, move to be after the Copy Bundle Resources phase. Add this code:

      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"
      
    • Replace MyAppName with your actual app's name, and the 1 after PreferenceSpecifiers to be the index of your Version entry in the Settings. The above Root.plist example has it at index 1.

    0 讨论(0)
  • 2020-11-29 15:52

    Using Ben Clayton's plist https://stackoverflow.com/a/12842530/338986

    Add Run script with following snippet after Copy Bundle Resources.

    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"
    

    Appending CFBundleVersion in addition of CFBundleShortVersionString. It emit version like this:

    By writing to $CODESIGNING_FOLDER_PATH/Settings.bundle/Root.plist instead of the one in $SRCROOT have some benefits.

    1. It dosen't modify files in working copy of repository.
    2. You don't need to case path to Settings.bundle in $SRCROOT. The path may vary.

    Testing on Xcode 7.3.1

    0 讨论(0)
  • 2020-11-29 15:57

    For me this was the easiest solution:

    Add new script build phase before Copy Bundle Resources step

    Shell: /usr/bin/env python

    Content:

    #! /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)
    
    0 讨论(0)
  • 2020-11-29 16:00

    With Xcode 11.4, you can use the following steps to display the app version in your application's settings bundle.


    Set $(MARKETING_VERSION) and $(CURRENT_PROJECT_VERSION) variables

    Note: if $(MARKETING_VERSION) and $(CURRENT_PROJECT_VERSION) variables appear for Bundle version string (short) and Bundle version keys in Info.plist, you can skip the following steps and jump to the next section.

    1. Open the Xcode project.
    2. Open Project Navigator (cmd1), select your project to reveal your project's settings then select the app target.
    3. Select the General tab.
    4. In the Identity section, change the Version field content to some new value (e.g. 0.1.0) and change the Build field content to some new value (e.g. 12). These 2 changes will create $(MARKETING_VERSION) and $(CURRENT_PROJECT_VERSION) variables in Info.plist file.

    Create and configure Settings Bundle

    1. In the Project Navigator, select your project.
    2. Select File > New > File… (cmdN).
    3. Select the iOS tab.
    4. Select Settings Bundle in the Resource section, then click Next and Create.
    5. Select Root.plist and open it as source code. Replace its content with the code below:
    <?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>
    

    Add a Run Script

    1. In the Project Navigator, select your project.
    2. Select the app target.
    3. Select the Build Phases tab.
    4. Click + > New Run Script Phase.
    5. Drag and drop the new phase somewhere above Copy Bundle Resources section. In this way, the script will be executed before compiling the application.
    6. Open the newly added Run Script phase and add the following script:
    version="$MARKETING_VERSION"
    build="$CURRENT_PROJECT_VERSION"
    /usr/libexec/PlistBuddy -c "Set PreferenceSpecifiers:0:DefaultValue $version ($build)" "${SRCROOT}/Settings.bundle/Root.plist"
    

    Launch app

    1. Run the product (cmdR) on device or simulator.
    2. On the device or simulator, once the app is launched, open Settings app and select your app in the list of third-party apps. The app's version should be displayed as shown below:


    Sources

    • How To Manage Plist Files With PlistBuddy
    0 讨论(0)
  • 2020-11-29 16:01

    These are the variables I had to use for a swift project with Xcode 12.2

    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"
    
    0 讨论(0)
提交回复
热议问题