User Defined Build Setting

Abhishek Kumar
2 min readMay 3, 2019

--

During development and deployment of app, you need to use 2–3 kind of server urls depending upon develop (staging mode), and production mode (deployment or market release).

So, How we can maintain 2 urls and make it automated without need to change some variable, or using some boolean or even macros everytime you need to point to particular server.

Best way is to use User defined build settings feature provided in Xcode.

The process involves 3 steps.

  1. Add user defined settings in your target’s build settings.
  2. Add a key in your Info.plist file that uses your recently defined build setting.
  3. Read the value via Info.plist in your code.

1) Adding Build Settings in Xcode

Select your project in the ‘Project Navigator’, your target, build settings and click the ‘+’ icon on the bar below to add user settings.

2. Adding a Key in Info.plist

Select the Info.plist file from the project navigator in Xcode. Hover on the last row and click ‘+’ to add another below. Enter the ‘ServerUrl’ in the key and $(SERVER_URL) in the value part.

Here, we are accessing the build setting that we just added. At run time, anything that is inside $() is replaced by its actual value in the build settings. In our case our SERVER_URL setting will be placed here. You can also notice other similar entries in the Info.plist like bundle identifier, bundle name etc.

3. Read the value via Info.plist

Bundle class provides a method to access values defined in Info.plist using corresponding keys.

let serverUrl = Bundle.main.object(forInfoDictionaryKey: “ServerURL”) as! Stringprint(serverUrl)

--

--