company-logo

iPhone: Using Pre-processor Directives for Device Testing

iPhone in Dock
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.

Related Services: iPhone Application Development, Custom Software Development

  1. Michou Reply

    As far as I’m concerned, I prefer this test

    #if TARGET_IPHONE_SIMULATOR
    // On simulator
    #else
    // On device
    #endif

    see U

  2. Paul Goracke Reply

    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.

  3. Ivan Moscoso Reply

    You are absolutely right- and thanks for pointing it out. After I initially posted this entry, I should have gone back and corrected this.

Leave a Reply

*

captcha *