Managing different server environment/configuration in preferences of iOS application
Managing different server environment/configuration in preferences of iOS application
Managing different server environment/configuration in preferences of iOS application

Managing different server environment/configuration in preferences of iOS application

Pramod Kumar
Jun 24, 2019 · 3 min read

As of being an application developer, every developer have faced the issue to release the build for different server environment/configuration like production, testing, development e.t.c.
It’s very usual that we’ve to manage these scenarios during our application development process.
All these environments refer to different server URLs, application name, application icons and may more.

So, when we need to release the build, our application need to point one of them environment.
In most cases, we make a global variable or enum to manage pointing the environment, then change the value of that variable and release the build.
But in iOS, it’s very time consuming thing to release the build 😄.

In this tutorial, today we’ll discuss how can we manage these all environments at one place 😊.

A very uncomfortable reason: It takes about 15–20 minutes to release a build from one environment, for 3–4 environment will take about 1 hour probably more. It’s a time taking process 🕜.
Using the plist, seems the good way to manage these environments. We can provide a option to change the environment in setting of our application. It can be managed by Settings.bundle

Image for post
Image for post
Steps to change the environment in settings app

By Settings.bundle:

Xcode provides the template for adding the setting bundle in our existing project.
Settings.bundle file used to add the UI components in the settings preferences of our application, can be configure by user to be used by the application.

To add a Settings bundle to your Xcode project:

  1. Choose File > New > New File.
  2. Under iOS, choose Resource, and then select the Settings Bundle template.
  3. Name the file Settings.bundle.
Image for post
Image for post
Steps to add Settings Bundle

After adding the bundle in the project, go to the project navigator in Xcode.
On project navigator, expand the Settings.bundle to see it’s files, you will find one folder en.lproj that used for localized strings.
another one is Root.plist file that contains all the preferences items, we can modify it according to your need.
In this file change the preferences items to the following:

Image for post
Image for post
Preferences plist file

In Root.plist file, find the item under the Preference Items with title Select Environment. It’s a multi value type item, we can give one array for titles and another one for values.
Items assigned in the titles will be shown to the user in the preferences of our application.
Items assigned in the values will be saved in the user default at the key specified in identifier (“currentEnvironment”)field.

Add a new swift file to the project and name it as you want. In my case I’ve named it as “PKSettingsBundleHelper”
Find it down here:

In this file, Create an Enum Environment and give the same cases as given in settings bundle value array.
we’ll access the value from UserDefaults, and create an object of Environment enum and then we can use it in out project any where.

Image for post
Image for post
Environment Demo

When you application is ready to launch to App Store, this type of preferences are not so appropriate to be visible. To remove it, you can use a Shell Script, adding a new Build Phase of type “Run Script”, past the below code in the script :

We did it.

Visit this link to download the complete project.

👩‍💻 !!! HAPPY CODING !!! 👨‍💻

Thank you for reading, please hit the recommend icon if like this collection 😊 . Questions? Leave them in the comment.

SwiftCommmunity

Learn Together, Grow Together

Pramod Kumar

Written by

http://pramodkumar.cf/

SwiftCommmunity

Apple developer can share about Xcode, Swift, Objective-C and SwiftUI or any suggestion/knowledge that might be occurred while development of application.

Pramod Kumar

Written by

http://pramodkumar.cf/

SwiftCommmunity

Apple developer can share about Xcode, Swift, Objective-C and SwiftUI or any suggestion/knowledge that might be occurred while development of application.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store