Working with Xcode configuration files
Learn how to manage different iOS build environments with .xcconfig files.
21 Feb 2022 · 3 min read
In the last article on Managing different iOS build environments in Xcode, we used Xcode's build settings tab to setup the build environments develop, staging and release in our iOS application. I recommend to check it out before reading this guide.
In this article, we are going to look at an alternative way to achieve the same results - with Xcode configuration files .xcconfig.
Let's jump in.
Creating configuration files
The files are empty by default.
Connecting configuration files with build configurations
As shown above, Xcode offers to select from a list of .xcconfig files we created.
Adding values to configuration files
The next step is to add values to the newly created files, for example the api url that is different for each environment.
API_URL = https://dev.api.com
API_URL = https://staging.api.com
API_URL = https://api.com
Update: when working with xcconfig files, a common issue is not being able to write URLs due to the double slash in http:// which is interpreted as a comment. A workaround is to use https:/$()/dev.api.com instead.
Accessing configuration values in code
And then accessing the value in code with:
Working with inheritance
To avoid redundancy, there is a possibility to inherit values from project to target level. Let's say, we want to use a different bundle identifier for each environment so they don't override each other when building.
For that, we create a configuration file, for instance called Common.xcconfig, and connect it with the project instead of the target.
In Common.xcconfig, we setup a reusable value for the bundle identifier as follows:
BUNDLE_IDENTIFIER = com.tanaschita.exampleapp
And reuse it in our configuration files with $(inherited):
#include "Common.xcconfig"BUNDLE_IDENTIFIER = $(inherited).debug
#include "Common.xcconfig"BUNDLE_IDENTIFIER = $(inherited).staging
That's a nice way to avoid repeating the same values in different files. We can also reference defined values in new ones with $(VARIABLE_NAME).
Using .xcconfig files initially requires a few additional steps to setup, but especially when the list of custom build settings is growing, it might be easier to manage them in separate files.
.xcconfig files also allow us to manage build settings independently from Xcode. They can be modified with any editor, are reusable and friendlier to use with source control.
Like to support my work?
Articles with related topics
iOS developer guide on the main aspects of Core Data
Understand the main concepts of the Core Data framework.
19 Sep 2022 · 5 min read
Managing different iOS build configurations in Xcode
Learn the possibilities to set up environments like debug, staging and release for your iOS app.
14 Feb 2022 · 4 min read
swift package manager
Quick Guide on Swift Package Manager for iOS
Learn how to use the Swift Package Manager with Xcode to add dependencies to your iOS project
17 Apr 2021 · 6 min read
swift package manager
How to add local Swift Packages to an iOS project
Learn how to add local dependencies to your iOS project by using Swift Package Manager and Xcode
17 Apr 2021 · 2 min read
swift package manager
How to add public libraries as Swift Packages to an iOS project
Learn how to add, update and delete remote dependencies using the Swift Package Manager and Xcode
16 Apr 2021 · 3 min read
Latest articles and tips
Cheat sheet on Combine operators for iOS development
Get an overview on Publisher operators in Combine.
21 Nov 2022 · 5 min read
Understanding date decoding strategies when working with JSON in Swift
Learn how to convert JSON dates to Swift's native Date type and vice versa.
14 Nov 2022 · 2 min read
How to handle non-optional Core Data properties in Swift
Explore the possibilities to use non-optional Swift values in Core Data.
07 Nov 2022 · 4 min read
Understanding different cache policies when working with URLRequest in Swift
Learn how to manage cache when sending HTTP requests in your iOS applications.
31 Oct 2022 · 4 min read
How to create NSManagedObject subsclasses for Core Data entities in Xcode
Learn about Xcode's code generation options when creating Core Data models in Swift.
24 Oct 2022 · 3 min read