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:
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.
Even though it’s easy, it has many drawbacks.
- Starting with the fact that we need to define the varobe everytime we want to switch the environments.
- 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.
- They can’t have different app names or app icons.
- Becomes difficult for the QA department to know what they are testing.
Using build scheme and build configurations
Knowing some basic terms:
- 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.
- Build Scheme – A collection of targets to build, a configuration to use when building, and a collection of tests to execute.
- 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.
- 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.
- The goal is to have three build configurations: debug , qa and release ; and a build scheme for each configuration.
- The first step is to add a label on the center of the file Main.storyboard. This indicates the current build configuration.
- 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
- Navigate to Current scheme (top bar of xcode)> Manage schemes. A new window pops up with current schemes. Rename the existing one to “Debug”.
- 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.
- Click on the current scheme button > Edit Scheme > check debug scheme to select
- New window will pop up showing the current scheme on the top left corner.
- Debug > Run > Select Debug as build configuration.
- Navigate to Archive (left menu)>select Debug as build configuration.
- Repeat the same for “QA” and “Release”
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.
- Navigate to File > New File > select Configuration Settings File > Name it Debug.
- 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.
- Navigate to Info.plist > add new row “Configuration” > Type– “String” > Value — “$(CONFIGURATION)”.
- 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.
- Navigate to ViewController.swift.
- 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.