Reputation: 759
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?
Upvotes: 2
Views: 9491
Reputation: 8115
For any devs using Swift version >= 4.1, it'll be better to use #if targetEnvironment(simulator)
. Reference: Target environment platform condition
Code:
extension UIDevice {
static var isSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
}
Upvotes: 1
Reputation: 23651
Note that newer macros were introduced in iOS 9 along with tvOS and watchOS, and the TARGET_IPHONE_SIMULATOR macro was deprecated at that time to help avoid confusion. From TargetConditionals.h:
TARGET_OS_WIN32 - Generated code will run under 32-bit Windows TARGET_OS_UNIX - Generated code will run under some Unix (not OSX) TARGET_OS_MAC - Generated code will run under Mac OS X variant TARGET_OS_IPHONE - Generated code for firmware, devices, or simulator TARGET_OS_IOS - Generated code will run under iOS TARGET_OS_TV - Generated code will run under Apple TV OS TARGET_OS_WATCH - Generated code will run under Apple Watch OS TARGET_OS_SIMULATOR - Generated code will run under a simulator TARGET_OS_EMBEDDED - Generated code for firmware TARGET_IPHONE_SIMULATOR - DEPRECATED: Same as TARGET_OS_SIMULATOR TARGET_OS_NANO - DEPRECATED: Same as TARGET_OS_WATCH
Upvotes: 15
Reputation: 318944
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
Upvotes: 2