XCode Conditional Compilation

I know the rest of the world has had this for years, and I know it’s in Flex Builder if you dig around hard enough, but seems like someone’s trying to keep it a secret. But conditional compilation in XCode is saving my life.

Big case in point: you have an iPhone simulator, which does a decent job showing most simple stuff. But it’s multitouch support is severely limited. And no accelerometer. So there are certain things you just have to run on the device. But compiling, installing on the device, and debugging from there takes several times longer than going straight to the sim.

In Gravity Pods, which I am developing for the iPhone, I have a user gesture where when the user touches on the gun, the aiming menu fades in. He can then aim and shoot. When she releases the gun, the menu fades out. Works great on the device, but sucks on the simulator because if you use your mouse to click on the gun, you can’t use it to click on the aiming menu. So, for the sim, I created an alternate gesture – pressing the gun toggles the menu. Press once, menu fades in, again it fades out.

To accomplish this,  you check the environmental variable, TARGET_IPHONE_SIMULATOR. If that’s defined, you are on the simulator. If not, you are on the device. So you go like this:

[c]#if (TARGET_IPHONE_SIMULATOR)
// you are in the simulator here. implement the toggle stuff.
#else
// you are on a real live iphone. keep it real
#endif[/c]

Love it, love it, love it.

You can also do the same thing with multiple targets. For iAttractor, I had a Lite version and a full version. I set an env var in the lite target and checked that the same way to add/disable features. Works very nicely.

This entry was posted in iPhone, Objective C. Bookmark the permalink.

8 Responses to XCode Conditional Compilation

  1. Flash CS4 has this built in, too, if you use it to publish your code. Here’s a screen shot of the place to add the conditional constants: http://twitpic.com/16tla

  2. darrelplant says:

    If you ever get the chance, check out the Unity 3D game system’s iPhone publishing demo video at http://unity3d.com/.

    What they developed is something called Unity Remote, which is an iPhone application that communicates over the network with the Unity iPhone development program sending user data (accelerometer and touch info) to the development system, then displaying a stream of images depicting the real-time simulation of the game on the iPhone. You can modify variables in the development application and see instant changes on the iPhone.

    The downside to this is, of course that until you make an actual build from Unity, what you see through the Remote is low-resolution JPEG images of the application executing on the computer running Unity rather than the actual build executing on an iPhone, but still it’s something to see. There are some other major drawbacks (at this time) to the Unity system for iPhone development, as well, but if you’re interested in 3D games it’s got some promise.

  3. sascha/hdrs says:

    Though it’s not conditional compilation but in ActionScript you know you could achieve the same effect by checking Capabilities.playerType and/or Capabilities.isDebugger.

  4. Rothrock says:

    The most impressive part is the user is male when he press the gun, but female when she releases it! Wow that is some powerful code. 🙂

  5. kp says:

    Rothrock. LOL. It’s a powerful gun. 🙂

  6. iknow says:

    Sascha, that’s not same effect… as you said yourself. Conditional compiling only compiles what is allowed. Checking runtime Capabilities is totally different thing…

  7. sascha/hdrs says:

    iknow, I know (haha nice pun) that’s it’s not the same … as I said. I just wanted to point out that in AS3 you can also run different code portions depending on the runtime type, even if all the code is compiled on the ‘surface’ it has the same effect.

Leave a Reply