Thomas Tempelmann
Thomas Tempelmann

Reputation: 12079

Xcode, iPhone: How to detect simulator target at compile time?

I wonder if, when building an iPhone app for the Simulator, there are special DEFINEs added that allow me to conditionally compile for this case?

If not, I'll have to add my own targets for this case, but I'd rather have an automatic way of detection.

Alternatively, is there a dynamic way to know when my code runs on the Simulator, I mean something that's documented? I've been searching the docs for a while now but had no luck yet.

Upvotes: 22

Views: 10564

Answers (2)

kaushal
kaushal

Reputation: 1573

@Update :
In iOS 9.0 SDK, TARGET_IPHONE_SIMULATOR is - DEPRECATED. use TARGET_OS_SIMULATOR instead of TARGET_IPHONE_SIMULATOR

#if TARGET_OS_SIMULATOR
  // Simulator code
#endif

from Swift 4.1:

#if targetEnvironment(simulator)
    // code for the simulator here
#else
   // code for real devices here
#endif

Upvotes: 14

Vladimir
Vladimir

Reputation: 170849

For compile-time check you need TARGET_IPHONE_SIMULATOR defined in TargetConditionals.h

#if TARGET_IPHONE_SIMULATOR
// Simulator code
#endif

For run-time check you can use for example -model method in UIDevice. For iPhone simulator it returns iPhone Simulator string (not sure about iPad simulator though)

Upvotes: 54

Related Questions