iPhone / xCode Conditional Compile for Localhost
When developing iPhone applications that use data/api’s in the cloud it becomes painful to run in development mode. Most of our applications talk to Ruby on Rails back ends via REST api’s. Having to change a variable or comment out a line of code every time you compile for a development, staging or production back end can become painful pretty quickly.
It is great that xCode allows conditional compiling and has some nice built-in ways to deal with pre-processing macros. Here are the steps to setting up a production vs. localhost connection via pre-processing conditional compiling.
1. Highlight the target in xcode IDE
2. Click the info button
3. Click the build tab on the info dialog
4. Click inside the pre-processor box to add a new macro (you will have to scroll down to find this option)
5. Click the plus button in the lower left corner to add an additional macro
6. Add USE_LOCALHOST as the macro name.
Now you have created a Pre Processor Macro to use in code.
We like to make a copy of that called DEBUG USE LOCALHOST (and then go and remove the macro in standard DEBUG). You could also copy the DEBUG one first then do steps above.
Now you should have a new configuration to choose from. From this point forward simply choosing DEBUG USE LOCALHOST vs DEBUG when going to compile will conditionally compile any code related to this macro.
Now you can create code to use that macro as part of a conditional compile. Depending on which configuration you use will determine which back end to attach to.
There might be better ways to do this, but this was simple and took a lot of pain out of compiling towards different back ends.