Managing different server environment/configuration in preferences of iOS application
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
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.
Adding the Settings Bundle
To add a Settings bundle to your Xcode project:
- Choose File > New > New File.
- Under iOS, choose Resource, and then select the Settings Bundle template.
- Name the file
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:
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 than we can use it in out project any where.
Hiding Settings bundle for Release:
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 :
👩💻 !!! HAPPY CODING !!! 👨💻
Thank you for reading, please hit the recommend icon if like this collection 😊 . Questions? Leave them in the comment.