Specifying Simulator Only Code

There are times when you need to write code that is only applicable when working with the simulator.

As an example, I was recently working with an application that required the user to take a picture with their iPhone. Once the picture was taken the application was to show the image to the user, at which point they would measure the size of an object in the image using various controls (movable lines) and a reference object in the image. Problem is, the simulator does not have much for camera functionality (obviously, there is no lens) so any picture taken shows up as a black image.

To get around this problem, I used the compiler directive below and inside the simulator section I would load an image and use the same throughout the application as if it was created from the camera.

#if TARGET_IPHONE_SIMULATOR
  NSLog(@"Running on Simulator");
#else
  NSLog(@"Running on Device");
#endif

Working with a directive such as this is really handy as you can move between testing with the simulator and a real device without having to change any code.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP Like Button Plugin by Free WordPress Templates