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.

Topics: , ,

Comments: 3 so far

  1. 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

  2. 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

  3. 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

Leave a comment

Powered by WP Hashcash

About Pathfinder

Follow the Blog

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email

      

    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com