Target iPhone Simulator Macro Not Working
Asked Answered
C

6

60

Using the TARGET_IPHONE_SIMULATOR macro results in the same constant values being defined in am application. For example:

#ifdef TARGET_IPHONE_SIMULATOR
NSString * const Mode = @"Simulator";
#else
NSString * const Mode = @"Device";
#endif

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
   ...
   NSLog(@"Mode: %@", Mode);
   ...
}

Always results in "Mode: Simulator" being logged. I'm currently running XCode 3.2.4 if that helps. Thanks.

Chichi answered 6/8, 2010 at 21:20 Comment(2)
TARGET_IPHONE_SIMUATOR has a typo (missing 'L'), and it shows up in the google search preview as such.Cambridgeshire
@wilsonmichaelpatrick fixedChichi
C
109

TARGET_OS_SIMULATOR is defined on the device (but defined to false). The fix is:

#include <TargetConditionals.h> // required in Xcode 8+

#if TARGET_OS_SIMULATOR
NSString * const Mode = @"Simulator";
#else
NSString * const Mode = @"Device";
#endif

Not sure when this was changed. I'm fairly sure it was possible to use 'ifdef' in the past.

Chichi answered 6/8, 2010 at 21:27 Comment(3)
This is a common mistake in many libs, e.h. here: github.com/domesticcatsoftware/DCIntrospect/issues/24. Thanks for sharing your solution!Bourse
Thanks for the topic; I thought I was going mad, or worse (forgetting my many years a C development).Interposition
TARGET_IPHONE_SIMULATOR is deprecated in iOS 9. Use TARGET_OS_SIMULATOR in XCode 7Supination
R
13

For me explicitly including TargetConditionals.h helped

#include <TargetConditionals.h>
Retouch answered 7/9, 2015 at 14:20 Comment(1)
Seems to me that from some specific Xcode version on (I am currently using 7.3.1) this is necessary.Perennial
D
6

Try TARGET_OS_SIMULATOR, as TARGET_IPHONE_SIMULATOR is deprecated.

Dyann answered 27/10, 2015 at 14:11 Comment(0)
R
3

I would try implement macro if its going to be used on different classes through out the app.

in pch file ,

#if TARGET_IPHONE_SIMULATOR
#define isSimulator() YES
#else
#define isSimulator() NO
#endif

and in any class I can check by calling isSimulator().

Rogatory answered 31/7, 2013 at 16:56 Comment(0)
R
1

For some reason TARGET_IPHONE_SIMULATOR doesn't work for me in xcode v6.4 . The snippet below works perfectly :

#if (!arch(i386) && !arch(x86_64))
  camera           = Camera()
#else
  camera           = MockCamera()
#endif
Religion answered 20/7, 2015 at 9:40 Comment(0)
Z
0

Swift:

#if targetEnvironment(simulator)
    showSimulatorOnlyError()
#endif
Zibet answered 15/7, 2022 at 4:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.