Target iPhone Simulator Macro Not Working
IphoneCocoa TouchIphone 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