I'd like to set the value of a variable in my Xcode project based on a command-line argument passed into xcodebuild
.
For instance, I'd like to run xcodebuild -project MyProj.xcodeproj -alltargets -configuration Debug ENV=2
and have it set a string named "rootUrl" in my code.
I'm expecting something similar to the following in my code:
#if (ENV == 2)
static NSString * const rootUrl = @"staging.url.com";
#elif (ENV == 1)
static NSString * const rootUrl = @"dev.url.com";
#else
static NSString * const rootUrl = @"prod.url.com";
#endif
But no matter the value of ENV
I pass into xcodebuild
, rootUrl
always gets set to "staging.url.com".
I also have ENV=$(ENV)
set in my Preprocessor Macros under Build Settings.
How can I achieve the desired behavior?
Try replacing ENV=2
with GCC_PREPROCESSOR_DEFINITIONS="ENV=2"
on your command line. I don't think you need to set the 'Preprocessor Macros' in the Build Settings as it will be overridden anyway.
See xcodebuild - how to define preprocessor macro? for more information.