Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Target iPhone Simulator Macro Not Working

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.

like image 694
Kevin Sylvestre Avatar asked Aug 06 '10 21:08

Kevin Sylvestre


4 Answers

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.

like image 121
Kevin Sylvestre Avatar answered Nov 06 '22 16:11

Kevin Sylvestre


For me explicitly including TargetConditionals.h helped

#include <TargetConditionals.h>
like image 40
Sebastian Avatar answered Nov 06 '22 14:11

Sebastian


Try TARGET_OS_SIMULATOR, as TARGET_IPHONE_SIMULATOR is deprecated.

like image 6
Michael Voong Avatar answered Nov 06 '22 15:11

Michael Voong


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

like image 3
user714236 Avatar answered Nov 06 '22 16:11

user714236