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

后端 未结 6 1479
無奈伤痛
無奈伤痛 2020-12-01 18:43

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:

         


        
6条回答
  •  春和景丽
    2020-12-01 19:14

    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);
    

提交回复
热议问题