I’d like to manage my project’s version information which is all scatterred througout multiple files, for example, .podspec
, .xcodeproj
and some .h
files.
What I’m wondering is that are there any ways to automatically change them all at once?
For now, I’m searching each of them and then change the value manually.
I think there would be the ways through CI/CD but couldn’t find the solution easily.
I’ve found a tool called agvtool but it seems it can’t manage files related to cocoapods.
I’d like to automate versioning like the Adjust SDK did in here, not sure they really did it automatically though.
It would be better if it can be done at the moment the commits pushed into the master branch.
It this possible? If you give me any hints, it would be really helpful. Thanks.
CodePudding user response:
There's probably no one solution that fits all needs, but one that works quite well is to use Xcode custom variables.
- Go to your project build settings (not just the one for a single target), press the
- Name this something like
APP_VERSION
and assign it the version value you want to use.- You can also split it up into major/minor/patch variables and then reference it like
$(APP_VERSION_MAJOR).$(APP_VERSION_MINOR).$(APP_VERSION_PATCH)
if you feel like it.
- You can also split it up into major/minor/patch variables and then reference it like
- Go to the build settings (project-wide or for your target) and search the "Preprocessor Macros" setting in the "Apple Clang - Preprocessing" section. Edit it and add
-DAPP_VERSION="\"$(APP_VERSION)\""
.- Now you can use it in C/Objective-C header and implementation files:
const char * version = APP_VERSION;
orNSString * const version = @APP_VERSION;
. - To use it in Swift, you need to expose it via your Swift import header (the "Objective-C Bridging Header" in the build settings).
- Now you can use it in C/Objective-C header and implementation files:
- In your
Info.plist
files, set the "Bundle version string (short)" (akaCFBundleShortVersionString
) to$(APP_VERSION)
.
If you want to override the version in your CI, simply pass APP_VERSION=…
to your call to xcodebuild
: xcodebuild … APP_VERSION=1.2.3
.
You often have two versions: the one you present to your user everywhere, aka CFBundleShortVersionString
. And then the "real" version, aka CFBundleVersion
. You'd just do the same as above, but with two different variables, like APP_VERSION_MARKETING
and APP_VERSION_INTERNAL
.