Xcode schemes and handling multiple environments

Nabin Shrestha
5 min readJun 11, 2021

When we are developing our apps we have one backend base URL for testing purpose and another one when pushing the app in live environment.

If you have been manually managing this by commenting out the testing phase base URL uncommenting the live base URL every time you archive and push your app to production, you know how much of a hassle it is. Sometimes you may even forget to do that and push the app pointed to test URL.

If this has happened to you before and you want to avoid such mistakes then here is a solution for you.

Configen:

I found this solution to be best for me as I had to use more than 2 different backend base URLs to work with.

Depending upon our need first we create schemes in our project. Schemes helps us to provide what targets to build, configurations to build them and much more.

When we create a Xcode project we only have one scheme by default.

Project with default scheme

Now we create our schemes like Development, UAT, Production etc. as per our need. So to create a scheme click on New Scheme and give it name and choose a target (if we have multiple targets) which you want to create a scheme for.

Creating a new scheme

Go ahead and click on OK. We have successfully created our scheme. Repeat the process for Release, UAT etc.

Now go to your project root folder and create a folder named configuration. We will use to to save our multiple environment files.

Download the executable file on this path https://github.com/theappbusiness/ConfigGenerator/blob/main/ConfigenDemo/configen and move it inside the configuration folder we created.

Now go to Xcode and add the folder configuration as a group inside the project.

Adding configuration folder as as group

After this, we are going to create plist files for our environments. Right click on newly added configuration group and click on New File on the menu. We’ll be presented with the template window, search for Property List and select it. Give it a name. We are creating configuration property list for Development scheme so we’ll be naming it development-config.plist. Repeat the same process for other schemes.

Configuration plist files

Create AppEnvironment.map file or download it from https://github.com/theappbusiness/ConfigGenerator/blob/main/ConfigenDemo/AppEnvironment.map

Move it inside the configuration folder we have in our project.

Add the file by right clicking on the configuration group in Xcode and setup the file contents like below:

AppEnvironment.map

Now we’re going to add Targets for the schemes to build respective configuration.

Go to File > New > Target.

Adding Targets

You’ll be presented with the window like shown below. Select Other Tab and choose External Build System.

External Build System

Give it a name depending upon the scheme and click on Finish.

Creating a target as per scheme

After clicking on Finish a target with the name you’ve given will appear like in the image below.

Newly added target

Do this for other schemes and we’ll end up with something like this.

Added all targets

After you’ve done this, check the right side to setup the configuration. In Build Tool field add “./configuration/configen” without the quotes. In Arguments field add “-p ./configuration/release-config.plist -h ./configuration/AppEnvironment.map -n AppEnvironment -o ./configuration” without the quotes.

The Arguments fields varies depending upon the target. Change the highlighted name just above as per the target and config plist.

After you’ve added the above details in all targets i.e. uat-target, development-target and release-target in our case, select a scheme and edit it.

Edit Scheme

After you tap edit you’ll be provided with the following window.

Scheme Details

Selecting Build, click on the + icon above Duplicate Scheme button and select target we added before as per the scheme we’re editing.

Select and click on Add. After this the previous window will show the target.

Release Scheme with added release-target

Note: Please add all the properties you’ve added in AppEnvironment.map in all the config.plist files i.e. development-config.plist, release-config.plist and so on.

Now, build the project.

Our project will build successfully if all the steps were correctly followed.

After our successful build, it will create a file named AppEnvironment.swift inside the configuration group. Right click on the configuration group and add the newly created file AppEnvironment.swift.

If we open and check that file, we will see the created configuration that we provided from the config.plist file depending upon the scheme we’ve built. Choose other schemes and build the project and see the changes in AppEnvironment.swift file.

AppEnvironment.swift

That’s it. Now you can access this and use this anywhere in the app like AppEnvironment.baseURL or any other variables generated dynamically on building.

Please feel free to clap if you appreciate this article. If you want the demo project for this, here is the github link. https://github.com/nabs107/ProjectHandlingMultipleEnvironments

--

--