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

后端 未结 6 1463
無奈伤痛
無奈伤痛 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:00

    Instead of using compile-time flags, you should use run-time check e.g. use NSClassFromString to see if a class exists because the same app can be installed on both devices.

    And because of the possibility of running the app in both devices, there isn't a built-in compile-time check whether it targets iPad or not.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-01 19:20

    Or -> just to be sure

    -(BOOL)isDeviceAniPad
    {   
    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
     BOOL deviceIsPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
     return deviceIsPad;
    #endif
     return NO;
    }
    
    0 讨论(0)
  • 2020-12-01 19:24

    Currently I didn’t find anything that would let you check if you are on an iPad, but I’m also not sure if Apple recommends runtime checks. Here’s an excerpt from Apple:

    In addition to your view controllers, any classes that are shared between iPhone and iPad devices need to include conditional compilation macros to isolate device-specific code. Although you could also use runtime checks to determine if specific classes or methods were available, doing so would only increase the size of your executable by adding code paths that would not be followed on one device or the other. Letting the compiler remove this code helps keep your code cleaner.

    However, there is no place I could find more specific information about conditional compilation macros.

    0 讨论(0)
  • 2020-12-01 19:26

    I think this will do

    -(BOOL)isDeviceAniPad
    {   
    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30200
      return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
    #endif
      return NO;
    }
    
    0 讨论(0)
  • 2020-12-01 19:27

    For multiple targets sharing the same project/code, I'm doing this by editing the C flags for my iPad target.

    With the [myapp]-iPad target chosen as the active target, pick Project -> Edit Active Target [myapp]-iPad. Search for "c flags" and double-click. Add a flag for "-D TARGET_IPAD". Now the symbol TARGET_IPAD will be defined only for your iPad target.

    Of course, this only works if you're using separate targets for iPad and iPhone. If you're running the same binary on both, obviously there's nothing the compiler can do for you. (However, the 3.2 SDK as of the end of January doesn't even support Universal apps yet.)

    (Edited; I was confused about the terminology of "Universal" apps etc.)

    0 讨论(0)
提交回复
热议问题