📜 ⬆️ ⬇️

Automatically update project build number in Xcode

When transferring the application to testers or end users, do not forget to increase the project version / build number so that in the future you can easily identify the installed application.

Memory, as we know, sometimes fails. Here comes to the aid of a simple and effective wonder-recipe for automatically setting the project assembly number.

A finished project with a short description is available on github: github.com/eshurakov/XcodeAutoBundleVersion

To install the application version, the developer has two variables:
CFBundleShortVersionStringThe marketing version displayed in the app store. Represents a string consisting of three numbers separated by a dot. Indicates the iteration of the released application. I always change this version manually.
CFBundleVersionBuild number . It is a monotonically increasing string consisting of one or several numbers separated by a dot. Indicates the iteration of an application being released or being developed.

Read more in the documentation provided by Apple: developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
')
We all use version control systems for development, so the obvious solution would be to use the latest revision number as the build number.


Setting up the project in 6 easy steps:


  1. Save the script to the project folder. This script calculates the build number and writes it to the transferred file.
  2. In the project, create a new Target with type Aggregate. I called my “Version”. In Build Phases, we add the Run Script Phase, from where we call our script:
     GIT_BRANCH="master" PREFIX="XC_" /bin/sh "${PROJECT_DIR}/Version.sh" "${PROJECT_DIR}/Version.h" 

    Script environment variables:
    Prefix
    Prefix of generated constants, may be missing.
    GIT_BRANCH
    The git repository branch for which the build number is considered. The default is “master”.
    PROJECT_DIR
    Path to the project folder. When you run the script from Xcode, this variable is already set.

    An example of the received header file:
     #define XC_BUILD_NUMBER 7 #define XC_BUILD_HASH @"c2acb9b" 

    We will use it to generate the Info.plist file of the project at the time of compilation. It can also be connected to a project and used at its discretion, for example, displaying a hash of the commit on the About Application screen.

  3. We make changes to the Build Settings main Target:
    Preprocess Info.plist File
    YES
    Info.plist Preprocessor Prefix File
    Version.h

  4. In Info.plist change the value of CFBundleVersion to XC_BUILD_NUMBER
  5. Add the newly created Aggregate Target as a dependency to our main Target.
  6. The Version.h file is added to the ignore list of the version control system.


Now, with each build of the project, the Version.h file will be updated, and along with it, Info.plist.

Test project on github: github.com/eshurakov/XcodeAutoBundleVersion

Source: https://habr.com/ru/post/162575/


All Articles