Xcode project configuration
In every iOS or OS X app there are many configurations like API base url, keychain service name, keys for various 3rd party services. Some of them are different for Debug and Production, which leads to numerous #ifdef
’s across all the project and so on.
Here I will show quick and convenient way to manage project compile-time dependencies in .xcconfig
’s.
Configs structure
Add new configs in Xcode: File > New > File… > Other > Configuration Settings File
.
Most configuration values will be stored in shared.xcconfig
:
And only ones that different to build configuration will be overridden, like in staging.xcconfig
:
Translation in preprocessor macro will happen in generator.xcconfig
:
You can see, for each value we adding VALUE='$(VALUE)'
to GCC_PREPROCESSOR_DEFINITIONS
string.
Swift
Bridging header is needed to access preprocessor definitions from Swift code. It can be empty if you don’t use any obj-c sources.
Project settings
Open Project > Info > Configurations
and select there corresponding configs. I have CocoaPods project, so I am adding this configurations on project-level, not target-level.
Usage
You can use macros everywhere in your code, you don’t have to import anything:
Also, I like to move some values from Info.plist (like bundle version and display name), so all my configs stored in one place:
Acknowledgments
Thanks to Paul Taykalo for sharing this way of managing configurations.