iOS developers work in three different environments before any solution is launched. These are Development, Stage, and Release. For every respective environment, there are separate elements, including;
- Application Icons
- Configurations, etc.
The Development environment is used to build a project, the Staging environment is used for beta testing, and the Release environment is used for launching the project.
Every environment is interconnected, which means that a developer needs to switch between them as and when required. Something as trivial as creating a build for an environment includes altering the server URL. This can be done by changing the hardcoded flag value in the constant file. Another way to do the same is by changing the Macros. As a result of these changes, the rest of the development activities become more complex.
The level of complexity in this exercise can be attenuated by using schemas and configurations in iOS application development. In this guide, we will talk about managing different environments in Xcode via configurations and schemas so that you can switch between different environments without running into hurdles and development errors.
Steps to Follow for Managing Environments Using Configurations and Schemas
1. Project Setup
- Open XCode and create a new project based on App Template.
- Name your Product name, organization identifier and select the required thing of your projects.
2. Adding Configurations
Every Xcode schema has two default build configurations;
By default, when you create a new project, the target is also created with two configurations.
These two default configurations will not suffice, especially when you want to experience seamless interaction between the three environments. To assure that we need to follow these steps;
- Double click on Debug and update the name with the Dev (Development).
- Click on the (+) button and create a new configuration by choosing Duplicate “Debug” Configuration and set the name as Stage.
- Repeat the steps above for adding Release configuration.
So now you have 3 Configurations (Dev, Stage, and Release), we can also add as many configurations as per the requirement.
3. Store Configuration in Info.plist
To ensure we can access configurations at runtime, store the configuration in Target’s info.plist. For this, add a new row in Info.plist and set the key Configuration and value as $(CONFIGURATION).
This configuration setting is used at runtime to execute the Build process by using the environment variables CONFIGURATION. With this setting, we can also set environment-specific keys and values by updating the target info.plist during the Buildprocess.
4. Define Schemes
Every iOS project you work on comes with one existing scheme. But we can also add new schemes to the same. This is done by adding a new scheme for every environment.
Click on the top left side and select the active scheme, and tap on New Scheme. Name the new scheme as per your requirement, let say Configuration-Dev, and repeat the same steps for creating a Stage and Release Scheme.
Further, we need to tell the scheme which configuration to use when the application is running. For this, select Configuration and click Edit to customize the scheme.
Now select the Run option from the left side and in the Info tab to set Release/Stage/Dev as per the scheme is selected from the list. Repeat the same step for the Test, Profile, Analyze, and Archive section.
5. Add Configuration Files
Once the Configuration is created, we need to add the relevant files. This is required to instruct or inform each configuration about what sort of apps, icons, or server URLs it possesses.
To add the files, create a new Configuration.swiftfile. Start with defining an enum name as Environment and define a case as per your defined schemes.
Defining a case defines an environment for the application. Now declare a computed property inside the enumeration, which returns a value of the currently selected environment.
Moving forward, define a structure name as Configuration, which has a static property environment of type Environment. This implementation is used to load the configuration from the Info.plist, which we have created earlier.
The code script above will return the selected scheme configuration from the target’s Info.plist.
You can also change the app icon to a different scheme. Go to Build Settings and search for the app icon, and set the app icon in Asset Catalog App Icon Set Name, and here you can set resources for each configuration.
This completes the process to create different environments and manage Xcode environment variables configuration. With the above process completed, you need to test the same.
To test the implementation of configuration, open ViewController.swift file and update viewDidLoad(). Follow the code script below.
Run the application, and you will see the output in the console. The results are seen with the selected scheme’s base URL and google token.
Once the above process is complete, you can run it with different environments and switch between them seamlessly. A good practice for running and managing the environments is to change the icon, app name, and bundle id to help the testers know which environment they are running. As we have created different configurations, it should be easier for you to run change the URLs plist.file and the schemas.
Devang S Bhatt
Latest posts by Devang S Bhatt (see all)
- Manage Different Environment And Configuration For iOS Project - November 29, 2021