How to Create Build Variant in iOS Application

Dwi Randy Herdinanto
Tunaiku Tech
Published in
5 min readJul 15, 2020

Introduction

During the development lifecycle of mobile applications, usually we want to build the application based on environment configuration, for example, if the app needs to connect to our backend, it is very common the app is connected to the backend on test environment during QA tests. The build will be configured with a test URL, after moving to the next stage we will use another URL for connecting to the staging/production environment.

The question is How to manage all these build variants easily with the same code base in Xcode? so in this post, i want to share and discuss how to create build variant with Xcode but before that, we have to know about the difference between build scheme and build configurations

Build Scheme & Build Configuration

Here is the simple explanation about Build Scheme & Build Configuration

  • Build Scheme
  • Build scheme is a blueprint for an entire build process and using build scheme Xcode will know what configuration is used to create the development, unit test, and production build
  • Build Configuration
  • Build configuration is a specific group of builds settings that can be applied to any target.

For a common project, this is good to have at least Development and Production variant

Create Xcode Configuration File (.xcconfig)

.xcconfig files are supplemental files that help to configure a specific build type. We can easily edit these files and these are plain text files that define and override the build settings for a particular build configuration of a project or target. we are going to create 3 Xcode configuration file for dev, staging, and production, open File -> New -> File and select Configuration Settings File

Here’s what configuration looks like:

// Development.xcconfigBASE_URL = http:/$()/dev.dwirandyh.comAPP_NAME = MyApp (Dev)APP_BUNDLE_ID = com.dwirandyh.myapp.dev// Staging.xcconfigBASE_URL = http:/$()/staging.dwirandyh.comAPP_NAME = MyApp (Staging)APP_BUNDLE_ID = com.dwirandyh.myapp.staging// Production.xcconfigBASE_URL = http:/$()/dwirandyh.comAPP_NAME = MyAppAPP_BUNDLE_ID = com.dwirandyh.myapp

Create the Build Configuration

First, let’s create a new Xcode project or we can use an existing project. Select on your project name after that Xcode will provide default two different configurations Debug and Release and we can create a build configuration with a different setting.

We will create a new configuration called it Staging we can do it by clicking “+” under configuration list then select Duplicate “Debug” Configuration and rename that build with Staging

Setting xcconfig Files to Build Configuration

Now we have three xcconfig files after that we need to set the appropriate file for each Configuration that we created previously.

Head back to the Project Info tab in the Editor view. Click the disclosure indicator for a given Configuration, and notice that the target has no configuration set. From the dropdown on the right, select the appropriate configuration file. Repeat until all the Configurations are set as follows:

Create Build Scheme

Using build scheme Xcode will know what configuration is used, because of that we are going to create 3 different build scheme for Dev, Staging, and Production so when we are on development process we can use dev scheme and so on, open manage schemes menu and duplicate existing schemes into three different scheme and add dev, staging, and production

Scheme Manager

Edit every scheme and set Build Configuration according to the Build Configuration we have made before. The result is three different versions of the app use corresponding build configuration

A scheme using Staging Build Configuration

Accessing Configuration Value

If you remember there are different APP_NAME and APP_BUNDLE_ID for each configuration file. We can use the configuration inside info.plist, so the application name and bundle id will be the same as a configuration file

Configure App Bundle ID
Configure App Name (Bundle Name)

With these changes, build and run the app using schemes from the Scheme selector. The result is three different versions of the app, each using a corresponding environment

Okay, so far we can use configuration settings in our app, but how do we access the configuration file via code? for example to get Base URL

Let’s begin to adding our configuration to our info.plist, because Xcode doesn’t provide BASE_URL key in the info.plist file we are going to add it manually by clicking add (+) sign and named it as BASE_URL after that set the value to $(Our Variable Name) which is $(BASE_URL)

guard let baseUrl: String = Bundle.main.infoDictionary?[“BASE_URL”] as? String else { return }print(baseUrl)

Final Thoughts

Xcode configuration file is a powerful way for configuring different build configurations, allowing you to manage build variants easily. Now we have set up to deliver a configurable app for different environments, Here is the GitHub project that contains all the configurations which we followed in this blog. Please let me know about your opinion in the comment section

Reference

--

--

Dwi Randy Herdinanto
Tunaiku Tech

A software developer that enthusiastic about mobile applications