Watch out for #ifdef TARGET_IPHONE_SIMULATOR

I just wanted to take a moment before the sands of time in the April hourglass run out for a friendly reminder for those iOS developers who are working in older Objective-C code bases. Watch out for #ifdef TARGET_IPHONE_SIMULATOR, as at some point, a newer version of the iOS SDK broke this check. In the current TargetConditionals.h file, TARGET_IPHONE_SIMULATOR resolves to either a 0 or a 1 (eventually, see below), which means that no matter where you run the code, TARGET_IPHONE_SIMULATOR will be defined, hence the code inside the #ifdef will always run.

The solution is of course to use the following line instead:

#if TARGET_IPHONE_SIMULATOR

Keep in mind that TARGET_IPHONE_SIMULATOR itself is marked as deprecated in the iOS 10 SDK TargetConditionals.h file, so I suppose to be future-proof, you should instead use:

#if TARGET_OS_SIMULATOR

BTW, Happy Honesty Day to my U.S. readers. All you need to know about Honesty Day is that I think it is one of the best days on the calendar.

Leave a Reply