Defining Your Own Settings in Xcode Build SettingsHassan Ahmed KhanBlockedUnblockFollowFollowingApr 16While developing an iOS app often times we have to work for different environments.
Like development or production.
The thing that most commonly change between these environments is the server url.
In order to develop or test the app we point our app to a development server.
And when we need to release our app to the AppStore, we need to switch the server url from development to production.
This looks rather simple if you do it manually.
You can keep your server url in a variable and every time you need to change the server url just change that variable and you are good to go.
But there are downsides to this approach.
You need to change the code every time you need to point your app to a different server.
If you are using a version tracking tool like Git, Every time you switch the url, your working directory is changed and you need to either reset it manually or commit it which results in more unnecessary work.
At any point of time, you are not sure where your app is pointing to and you need to look into the code to confirm.
To avoid all this, the easiest and quickest solution of this problem is to introduce your own settings in Xcode build settings of your project.
Let’s see how it’s done.
Get StartedThe process involves 3 steps.
Add user defined settings in your target’s build settings.
Add a key in your Info.
plist file that uses your recently defined build setting.
Read the value via Info.
plist in your code.
Adding Build Settings in XcodeSelect your project in the ‘Project Navigator’, your target, build settings and click the ‘+’ icon on the bar below to add user settings.
Name your new setting ‘SERVER_URL’.
Open the details of your setting by clicking the arrow.
Double click the field against ‘Debug’ and ‘Release’ to add dev and prod url respectively.
Your settings will look like this finally:Now, lets move towards the second part, i.
adding a key in Info.
Adding a Key in Info.
plistSelect 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.
Your final Info.
plist file will look like this: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.
So the hard part is over.
Now comes the easy part, i.
reading our settings from the plist in our code.
Read the value via Info.
plistBundle class provides a method to access values defined in Info.
plist using corresponding keys.
let serverUrl = Bundle.
object(forInfoDictionaryKey: “ServerURL”) as!.Stringprint(serverUrl)Add the above snippet in the applicationDidFinishLaunching method of your AppDelegate to test.
Run the app on simulator or device to verify that you get the dev url.
You will get the prod url when you create a release build.
But to test it now you can do a little hack.
Edit the scheme of your project to load release configurations instead of debug when run from Xcode.
Now when you run the app, it will log prod url.
That’s It!!!Its that simple to change the server url at run time with a little help of Xcode’s build settings and Info.
The url is set automatically depending if the app is being debugged or if it’s been released.
This approach is simplistic and you can add more variables according to your requirement.
Note:If you have more than two environments this approach might not work and you might need to add separate build configurations and project schemes which is a topic I will explore next in the recent future.
.. More details