📜 ⬆️ ⬇️

Automatically increment build number in Xcode

Users and testers can find errors that you probably already fixed. Sometimes users use the old version of the application, sometimes your fixes are not as good as you thought. In both cases, a small unique version number displayed in the application can save hours of your work.

It can be done

There are a dozen ways to do this that can be found on Google. But, unfortunately, not all of them work in Xcode 3.2 and Xcode 4, while others require a lot of crutches, even running external scripts in Perl or Python. Using avgtool seems unnecessary in most cases. There must be an easier way to do this, and there is such a way.

All we want is to have the build number in the Info.plist file, from where we can read it and display it in the application. We also want this number to automatically increase with each new build of the project.
')
Add a key called CWBuildNumber to our Info.plist file and set it to some starting value, for example “0”. You can read this value in the app using the construct:

NSString* buildNumber = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CWBuildNumber"];

Xcode 3.2 and Xcode 4 allow us to run scripts at runtime for any purpose. Unfortunately, Xcode 3.2 and Xcode 4 run them in different ways. The environment variable PROJECT_DIR will save the day ! We need to get the build number, increase it and overwrite the Info.plist file of our goal during the build. Just paste the following code into our goal run script:

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CWBuildNumber" ${PROJECT_DIR}/TestIncrement/TestIncrement-Info.plist)
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CWBuildNumber $buildNumber" ${PROJECT_DIR}/TestIncrement/TestIncrement-Info.plist

* translator's note : change the path to the Info.plist file in the above code to your own path.

Conclusion

Happy testers, happy developers. Perhaps even users are happy if you display the full version, including the build number, in the final product.

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


All Articles