How to synchronize versions and build numbers across different targets
If you ever developed an extension for iOS, you are most likely to be familiar with the following errors when uploading the app in the App Store:
CFBundleVersion Mismatch - The CFBundleVersion value '1' of extension 'ExtensionApp' does not match the CFBundleVersion value '2' of its containing iOS application 'Main.app'.
CFBundleShortVersionString Mismatch - The CFBundleShortVersionString value '1' of extension 'ExtensionApp' does not match the CFBundleShortVersionString value '2' of its containing iOS application 'Main.app'.
If you have only one extension it's not that difficult to keep them synchronized, but things get tricky when you add more than one extension. In my case, I have a project that has 6 targets and I need to keep the synchronized.
Since we are programmers, and our job is to solve problems, I thought that it must be a better way to synchronize the target's versions! The first thing that came to my mind was to use Run Script Phase
but then I thought it will get messy for 6 different targets.
After digging around, I found a much better and much cleaner solution. We will use the User-Defined setting that they will be used by all of the targets. All we have to do is go to the project's Build Settings
, click on the plus sign and add two settings and call them APP_VERSION
and APP_BUILD
.
You will find the newly added settings at the bottom of the Build Settings
tab. Set the APP_VERSION
to match the main target's version (eg. 3.4.1
) and the APP_BUILD
to match its build number (eg.15
).
These settings are accessible throughout the project using the $(APP_VERSION)
and $(APP_BUILD)
variables. Now, we need to go through each target's Info.plist
file and set the Build Version
value to $(APP_BUILD)
and the Build Version String (short)
to $(APP_VERSION)
. From now on, you should be changing the version and the build number from the Build Settings
.
That's it!
Happy coding!