Technological competence in software development means having adequate experience & knowledge of methodological traits like Development, QA, Beta, and Production. For these various concepts, there are multiple server URLs, app icons, and configurations. 

Settings and Configurations for iOS Project

It is very obvious that you have to deal with managing these scenarios during an application development lifecycle gradually. To manage these multiple environments during the development of an iOS project is one of the best practices that a developer can ingest. 

In the process of software development and its lifecycle, you often come across juggling between these server URLs, enabling-disabling logging and other build settings that are needed to be changed from Development, Staging and Production environments.

For example, if your app needs to connect to the server which has three different base URLs 

Developmentwww.dev.com

Staging:  www.staging.com

Productionwww.production.com

Changing these URL’s in your code every time for creating a build for a specific Environment can be tedious and error-prone.

So now we will have a look at how we can efficiently manage these Build variants, which change as per your Build Environment in Xcode.

Xcode Project

Follow the steps mentioned below for managing different Build settings for different Environment to your Existing or New Xcode Project.

Add Multiple Configuration

Select Project Navigator on Left Panel → Select Info

Select Configuration

  • Rename Debug to Development
  • Rename Release to Staging
  • Click on + → Select Duplicate Staging Configuration

Manage Different Build Settings and Configurations-2

Add Multiple Config Files

Select Project Navigator on Left Panel → Add New File → Configuration Settings File

Add .xconfig file for every configuration created (Development, Staging, and Production)

Manage Different Build Settings and Configurations

Select Project Navigator on Left Panel → Select Info

Select Configuration files for your Project (Refer to the screenshot below)

Manage Different Build Settings and Configurations-4

If you use Cocopods, you’ll have to delete the Podfile.lock, .xcworkspace file, and the Pods/ directory. Make sure you are not deleting the Podfile.

Go to Terminal run pod install

You will get some warnings after running the pod install command 

Manage Different Build Settings and Configurations

To fix this problem: Open the recently created .xcworkspace file and attach the .xcconfig path for Cocoapods in your own .xcconfig files (Development, Staging, and Production). 

Manage Different Build Settings and Configurations-6

Scheme Creation

Select Project → New Scheme, create a scheme for your configurations (Development, Production, and Staging).

Delete existing schemes that were created by Xcode by default. 

Manage Different Build Settings and Configurations-7

Manage Different Build Settings and Configurations-8

After adding all the schemes, your Manage Scheme should look like below. Apart from this also make sure you select Shared for all the schemes and under Container select ProjectName Workspace (This is required if your project uses Pods )

Manage Different Build Settings and Configurations-9

Make sure that you are targeting XCode project name. Structure your scheme to resemble your configuration.

Manage Different Build Settings and Configurations-10

Setting environment-specific keys

We need to declare environment-specific keys and values in the Info.p list file.

The values for these environment-specific keys need to be defined in respective .xconfig files. 

Example:

base_url = www.dev.com 

log_enabled = true


Manage Different Build Settings and Configurations-11

Manage Different Build Settings and Configurations-12

Manage Different Build Settings and Configurations-13

Manage Different Build Settings and Configurations-14

Accessing environment-specific keys 

Manage Different Build Settings and Configurations-15

Accessing keys in code

Environment().configuration(Plist.loggingEnabled)
Environment().configuration(Plist.baseUrl)

Conclusion

Configuration is an essential part of most applications. By implementing an adaptable structure for providing configuration values, modifying them based on the environment, we can have a clean separation of code & configuration. It is always better if you spend a bit more time configuring these things at once rather than constantly revisiting & correcting the things in the future. 

Download Source Code