How to Configure Xcode Projects for Different Environments

Xcode created by Apple, is an integral part of the iPhone app development agency. The iPhone app development agency created Xcode for the developers to build apps for  iOS.

Being a developer one might be familiar with concepts like development, production, QA, and beta. During the app development stages, one needs to deal with these scenarios. In a hypothetical app that you are developing, that uses Firebase for storing and retrieving data. You have created your app and released it. As the process of further development, you like to introduce a new feature. For testing your app against the database, it might be that you’re testing against the production database. But what we want is to test it with a development database. So it is a good idea to separate into branches: one for the development database and one for the production database. This is called configuring environments in Xcode.

Each of the configurations consist of different server URLs, app names, app icons etc.

How to Configure Xcode Projects

There are mainly two ways to configure any Xcode project for different environments:

Setting global variable:

  • Method:

This is the easiest of the two ways. We can create an enum with cases development and production. Now we define a global variable of enum that tells the current environment. Then we need to use something like a switch to get the defined value of this variable and accordingly change the settings. The variable needs to be redefined every time we want to change to the environment and then build the app. This makes the code full of switches or if-else and not a sustainable approach for large scale projects.

  • Drawbacks:

Even though it’s easy, it has many drawbacks.

  1. Starting with the fact that we need to define the varobe everytime we want to switch the environments.
  2. Each time we want to test it we need to uninstall every time you change the environment, as they can’t have two bundle ids.
  3. They can’t have different app names or app icons.
  4. Becomes difficult for the QA department to know what they are testing.
Read Also :   Looking for a Compact Soundbar? Check the Bose Solo 5

Using build scheme and build configurations

Knowing some basic terms:

  1. Target – Specifies an app to build and contain the instructions for building the app from a set of files from a project or workspace. It let you specify an app icon, a source file, a bundle id for that target. Needed for different applications.
  2. Build Scheme – A collection of targets to build, a configuration to use when building, and a collection of tests to execute.
  3. Build Configuration – specific group of build settings that can be applied to any target.

If we have different environments for the same app then we need to use build schemes and build configurations. Setting them up is quite handy and prevents wasting time in the future.

Now we are creating an application with only a label. Its text property indicates the build scheme used.

  1. Create a new single view app. By default, when you create a new project, a target is also created, with two build configurations: debug and release.
  2. The goal is to have three build configurations: debug , qa and release ; and a build scheme for each configuration.
  3. The first step is to add a label on the center of the file Main.storyboard. This indicates the current build configuration.
  4. Now navigate to the project editor and choose the project. You can see debug and release configurations. Click plus sign below the configurations and select “Duplicate Debug Configuration”. Rename it to “QA”.

Creating and modifying Schemes

  1. Navigate to Current scheme (top bar of xcode)> Manage schemes. A new window pops up with current schemes. Rename the existing one to “Debug”.
  2. Add two new schemes “QA” and “Release” by clicking the plus sign at the bottom left. Check the “Shared” box to on and exit the window.
  3. Click on the current scheme button > Edit Scheme > check debug scheme to select
  4. New window will pop up showing the current scheme on the top left corner.
  5. Debug > Run > Select Debug as build configuration.
  6. Navigate to Archive (left menu)>select Debug as build configuration.
  7. Repeat the same for “QA” and “Release”
Read Also :   5 Things To Know About Voice And Data Technology Solutions

Click to read on how to go about with them.

With this the configurations are created and defined. Now we need to tell each configuration their app icon, server url, etc.

  1. Navigate to File > New File > select Configuration Settings File > Name it Debug.
  2. Repeat the same for “QA” and “Release” configurations.

Each file contains key-value pair’s specific for each configuration. Now we are adding a variable that indicates the configuration we are on.

  1. Navigate to Info.plist > add new row “Configuration” > Type– “String” > Value — “$(CONFIGURATION)”.
  2. The value must be the name of the variable in the config file, between parenthesis and preceded by a “$” sign. You can add as many variables as you need.
  3. Navigate to ViewController.swift.
  4. Add this code below “viewDidLoad”
let config = Bundle.main.infoDictionary?[“Configuration”] as? Stringlabel.text = config

       5.Do this for all three.

 Author Bio –

Hermit Chawla is a MD at AIS Technolabs which is a Web/App design and Development Company, helping global businesses to grow by Global Clients. He love to share his thoughts on Web & App Development, Clone App Development and Game Development.

Leave a Reply

Your email address will not be published.