iPhone: Using Pre-processor Directives for Device Testing

Updated. (see comments below)
There may be cases where a certain setting needs to be tweaked based on whether you test on the device or simulator. It took me a while to find the pre-processor directive to detect whether or not the current build targets the iPhone device or the simulator, but with a bit of searching through the GTM code, I found everything I needed in "TargetConditionals.h". An obvious place, in retrospect, but I wasn't able to find this information easily elsewhere on the web, so I mention it here in case you find it useful.
Here's an example of how I've used it:
#if TARGET_CPU_ARM#if TARGET_IPHONE_SIMULATOR // Device will hit external server over cell network NSString const *ROOT_URL = @"http://www.external-site.com"; #else // Simulator will hit local server over LAN NSString const *ROOT_URL = @"http://localhost:8667"; #endif
This kind of tweaking is particularly important when the device itself can not participate on the local network, but needs to access some kind of external environment (or proxy server). Conversely, targeting the simulator to use a local 'development' server is often faster when deploying and debugging issues in your application, particularly if you are developing the server component in tandem.
Another example involved certain audio playback features which work fine on the device, but run into hiccups running on the simulator. Since in this case the functionality is not critical to test, I ignore it in the simulator.
Comments: 3 so far
Leave a comment
About Pathfinder
Follow the Blog
-
Get a monthly update on best practices for delivering successful software.
Subscribe via email
Subscribe via RSS
Categories
Topics
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
Blogroll
Recent
- Elements of Testing Style
- Aesthetics and Web Design
- Asterisk-Java Testing with Groovy
- 3 Misuses of Code Comments
- Fluently NHibernate
- Digging a Hole and Covering it with Leaves — The Software Development Version
- The Importance of User Experience - Do You Understand It in Your Bones?
- Writing Your Own Protocol With NSURLProtocol
- What’s In Your Dock: iPhone edition
- Feature Fatigue

As far as I’m concerned, I prefer this test
#if TARGET_IPHONE_SIMULATOR
// On simulator
#else
// On device
#endif
see U
Comment by Michou, Monday, February 23, 2009 @ 12:51 pm
The officially supported mechanism for this seems to be TARGET_IPHONE_SIMULATOR for simulator-vs-device and TARGET_OS_IPHONE for Cocoa Touch-vs-desktop. These are detailed in the iPhone Development Guide.
TARGET_CPU_ARM works for now, but makes assumptions about the cpu/environment relationship that may not always hold true.
Comment by Paul Goracke, Wednesday, February 25, 2009 @ 11:10 am
You are absolutely right- and thanks for pointing it out. After I initially posted this entry, I should have gone back and corrected this.
Comment by Ivan Moscoso, Friday, March 6, 2009 @ 12:06 am