Search code examples
iphonexcodeapplication-settingssettings-bundle

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


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 contains a fragment like this...

     <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>

and I would like to replace the "VersionValue" string at build time.

I have a script that can extract the version number from my repository, what I need is a way to process (pre-process) the Root.plist file, at build time, and replace the revision number without affecting the source file.


Solution

  • I managed to do what I wanted by using the pListcompiler (http://sourceforge.net/projects/plistcompiler) open source porject.

    1. Using this compiler you can write the property file in a .plc file using the following format:

      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. I had a custom run script build phase that was extracting my repository revision to .h file as described by brad-larson here.

    3. The plc file can contain preprocessor directives, like #define, #message, #if, #elif, #include, #warning, #ifdef, #else, #pragma, #error, #ifndef, #endif, xcode environment variables. So I was able to reference the variable kRevisionNumber by adding the following directive

      #include "Revision.h"
      
    4. I also added a custom script build phase to my xcode target to run the plcompiler every time the project is beeing build

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

    And that was it!