iOS Configurable Environment Settings

Why it is important?

SANDEEP RANA
3 min readFeb 24, 2020

Skip this section if you already know the reason to do it.

On average, the process of Changing Configuration, building and deploying an app to the TestFlight takes around 1 Hour 10 Minutes (It varies with the number of files). So, now think you have to deploy 4 App build for 4 Configuration Settings. Yeah, I know, It’s a painful process.

And It’s a nightmare for the Backend team If they want to test something on localhost and then on the different environment for validation with some real data.

Let’s Do it

  1. Add a Settings.bundle to your Project. ⌘ + N

2. Expand Settings.bundle and Open Root.plist file

3. Add the required settings in plist file. In our case, we needed a customizable API base URL, Flexibility to switch between Phone’s Current Location, ability to input Custom Location and some Predefined locations for convenience.

Here is Apple’s Official Doc to understand the available control types till 2020 There are 7 Control Types:

For a Crash Course Follow Me 🍻😉

7 Types of Controls:

Source: Apple Documentation

This is how my Root.plist(Source code viewer) file looks after I added the required Control Types

Root.plist Gist

Here Starts the Fun part

You get and set preference values using the methods of the UserDefaults class.

So now to access the value of baseURL We can do

let baseUrl = UserDefaults.standard.string(forKey: KeysM.baseURL.rawValue) ?? “https://prod.com"

Same for Location we can do

let location = UserDefaults.standard.string(forKey: KeysM.location.rawValue) ?? “NewDelhi"

A Developer’s worst nightmare!

Puting testing configuration in Production builds

To avoid that We can do a bunch of things but the best one I personally like is Creating Targets for Test and Prod

Add Settings.bundle to only Test Target and uncheck it for Prod Target

Note: If you are using Objective-C too in your project. Then you might need to change your Objective-C Generated Interface Header Name to $(PROJECT_NAME)-Swift.h So that -Swift.h is same for both of the Targets

It can be changed in

Select Project > Build Settings > Search Objective-C Generated Interface Header Name > replace $(MODULE_NAME)-Swift.h with $(PROJECT_NAME)-Swift.h

Result!

That’s it. We did it!🤜🏻🤛🏻

Conclusion

Here we got an understanding of Settings.bundle and how to use them for User Preferences or Environment Settings. Essentially we have to do something new on the coding side, little experiments, so we can learn something new.
I hope you got an understanding of Settings.bundle 😊✌🏼

Thanks for reading 🙌🏼

If you have any query regarding this tutorial? | If you think you can do more simple way or little bit more extra things with this stuff please let me know questions, feedback or comments — on Twitter

--

--

SANDEEP RANA

is a Software Engineer currently working Ola Cabs, Bangalore. Along with iOS, have a bit of experience with psychology and atomic structure.