Is there a specific Xcode compiler flag that gets set when compiling for iPad?

Rei picture Rei · Jan 29, 2010 · Viewed 9.5k times · Source

Is there a specific Xcode compiler flag that gets set when compiling for iPad?

I want to conditionally compile iPad vs iPhone/iPod Touch code for example:

#ifdef TARGET_IPAD
   code for iPad
#else
   code for iPhone
#endif

I know there is already TARGET_OS_IPHONE and TARGET_CPU_ARM in TargetConditionals.h but anything that easily and specifically targets iPad?

-Rei

Answer

user195925 picture user195925 · Mar 22, 2010

The correct API to use for run-time checking of iPad vs. iPhone/iPad Touch is:

BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

The UIDevice header filer also includes a convenient macro, UI_USER_INTERFACE_IDIOM(), which will be helpful if your deployment target is < iPhone 3.2.

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)

So you could just say, negatively:

BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);