Back to Home

We generate the version and build number on the iOS application icon

ios · xcode · utility

We generate the version and build number on the iOS application icon



    During the development process, the application is tested, and it happens that you need to know which version of the application and with which build number the tester / other participant in the process has installed. For example, the bug has already been fixed, but the person has not yet installed the new build and complains that nothing has been fixed.

    Often this information is hidden somewhere, for example, in the Yandex.Market application, you need to poke in the Cabinet section and select the "About application" item:

    Example

    For convenience (or because this screen is not ready yet), you can bring this useful information directly to the application icon.

    1. We put the necessary packages from Homebrew:

    brew install imagemagick
    brew install ghostscript
    

    2. Open our project in Xcode, open our Images.xcassets, select AppIcon and see where the files with the icons are located:



    We see the file names, remember:



    In my case, I will only touch on the icons for Retina iPhones.

    3. Open Xcode, go to the settings of our target in the Build Phases section, add New Run Script Phase:



    4. Insert the code into the Run Script block:

    # если у нас релизная сборка, то нам нужна обычная иконка
    if [ $CONFIGURATION = "Release" ]; then
    exit
    fi
    # номер версии
    version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFOPLIST_FILE}"`
    # номер билда
    build=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}"`
    # если нужно, можно взять имя ветки из git
    branch=`git rev-parse --abbrev-ref HEAD`
    # функция генерации иконки
    function processIcon() {
        export PATH=$PATH:/usr/local/bin
        base_file=$1
        target_icon_name=$2
        base_path=`find ${SRCROOT} -name $base_file`
        if [[ ! -f ${base_path} || -z ${base_path} ]]; then
        return;
        fi
        target_file=`echo $target_icon_name | sed "s/_base//"`
        target_path="${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/${target_file}"
        width=`identify -format %w ${base_path}`
        echo $target_path
        echo $target_file
        convert -background '#0008' -fill white -gravity center -size ${width}x40 -pointsize 26\
        caption:"${version} (${build})"\
        "${base_path}" +swap -gravity south -composite "${target_path}"
    }
    # запускаем генерацию
    processIcon "ewrw_120-1.png" "[email protected]"
    processIcon "ewrw_180.png" "[email protected]"
    

    Done. When the project is assembled, if the application icons are in Images.xcassets, then inside file.app they have names like [email protected], [email protected], etc. after the build. These names are used in the last 2 lines. You just need to substitute the file names of your icons.

    As a result, when we collect the build build, the icon is replaced with a new one, with the version number and build number superimposed below:



    I hope someone will find this trifle useful.

    PS I took the information and dopped a little from here (there for the option when assets are not used).

    Useful (including in the comments): “Automatically increase build number in Xcode”

    Read Next