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:
CFBundleShortVersionString
The 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.
CFBundleVersion
Build 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.
For SVN users, everything is simple, the revision number is in the clear.
For GIT users, things are not so simple; we cannot use the last commit hash because of restrictions on the format of the assembly number. But there is a solution! Assuming that the project has a stable branch, whose history is not unwound back, we will use the number of commits in this branch as the build number.
Build number:
> git rev-list master | wc -l > 43
Hash commit by build number:
> git rev-list --abbrev-commit --reverse master | sed -n '43 p' > 5a09ebc
Unfortunately, I did not take care of the users of other version control systems :)
Setting up the project in 6 easy steps:
Save the script to the project folder. This script calculates the build number and writes it to the transferred file.
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:
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.
We make changes to the Build Settings main Target:
Preprocess Info.plist File
YES
Info.plist Preprocessor Prefix File
Version.h
In Info.plist change the value of CFBundleVersion to XC_BUILD_NUMBER
Add the newly created Aggregate Target as a dependency to our main Target.
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.