在编译iPad时是否有一个特定的Xcode编译器标志?

Rei*_*Rei 13 ipad iphone-sdk-3.2

在编译iPad时是否有一个特定的Xcode编译器标志?

我想有条件地编译iPad与iPhone/iPod Touch代码,例如:

#ifdef TARGET_IPAD
   code for iPad
#else
   code for iPhone
#endif
Run Code Online (Sandbox Code Playgroud)

我知道在TargetConditionals.h中已经有TARGET_OS_IPHONE和TARGET_CPU_ARM,但是任何容易且专门针对iPad的东西?

-Rei

小智 16

用于iPad与iPhone/iPad Touch的运行时检查的正确API是:

BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
Run Code Online (Sandbox Code Playgroud)

UIDevice头文件管理器还包括一个方便的宏UI_USER_INTERFACE_IDIOM(),如果您的部署目标是<iPhone 3.2,这将非常有用.

#define UI_USER_INTERFACE_IDIOM() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone)
Run Code Online (Sandbox Code Playgroud)

所以你可以说,消极地说:

BOOL deviceIsPad = (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone);
Run Code Online (Sandbox Code Playgroud)