As I have built more iOS apps over the last couple of years one of the biggest struggles I have had is the switch between development and release server APIs. More than a few times I have released an app pointing at the dev API. Not only is this an embarrassment, but it creates a logical nightmare in trying to redirect traffic from the dev environment to the live. Needless to say, there has to be a better way.
Over the weekend with a bit of research— which has me kicking myself for not doing it sooner— I found the best solution in Preprocessor Macros and User-Defined Settings. They are easy to set up and easy to use.
- Go to your Build Settings
- Go to the Editor menu, Add Build Setting, Add User-Defined Setting
- Give your setting a name, mine is called APIURL
- These settings have a Debug and Release value: set both
- Search in Build Settings for Preprocessor Macros
- Add
APIURL=\@\"${APIURL}\"
to both Debug and Release
You will now have a variable available anywhere in your code called APIURL. When compiling your app in Debug mode, you get the Debug value. When compiling your app for Release, you get the Release value. No more mistakes.