Search code examples
iosobjective-cios-simulator

iOS: Handling TARGET_IPHONE_SIMULATOR macro


My environment: ObjectiveC using Xcode 6.4 in OS X El Captain (10.11.1). In Xcode, target is set to iOS 8.

TARGET_IPHONE_SIMULATOR always resolves to true in the code below even when I select iPad2 as iOS simulator.

#if TARGET_IPHONE_SIMULATOR
  // block of code
#endif

Shouldn't TARGET_IPHONE_SIMULATOR be set to false when selecting iPad2 as an iOS simulator?


Solution

  • That macro is true for any simulator build. The macro existed long before the iPad came along. Back when "iOS" was "iPhone OS".

    So think of it as "TARGET_IOS_SIMULATOR".

    It's used when you have something in your code that should only be compiled when building for a simulated iOS device.

    If you need something to run differently between the iPhone simulator and the iPad simulator, you may want something like this:

    #if TARGET_IPHONE_SIMULATOR
        // This code is only for a simulator
        if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
            // iPhone/iPod touch simulator
        } else {
            // iPad simulator
        }
    #endif