Target iPhone Simulator Macro Not Working

IphoneCocoa Touch

Iphone Problem Overview


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.

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

For me explicitly including TargetConditionals.h helped

#include <TargetConditionals.h>

Solution 3 - Iphone

Try TARGET_OS_SIMULATOR, as TARGET_IPHONE_SIMULATOR is deprecated.

Solution 4 - Iphone

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().

Solution 5 - Iphone

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevin SylvestreView Question on Stackoverflow
Solution 1 - IphoneKevin SylvestreView Answer on Stackoverflow
Solution 2 - IphoneSebastianView Answer on Stackoverflow
Solution 3 - IphoneMichael VoongView Answer on Stackoverflow
Solution 4 - Iphoneuser714236View Answer on Stackoverflow
Solution 5 - Iphoneohad serfatyView Answer on Stackoverflow