#if check (preprocessor macro) to differentiate between iPhone and iPad

后端 未结 5 1132
别那么骄傲
别那么骄傲 2021-02-04 12:13

Is there a build preprocessor macro I can check, with #if or #ifdef to determine if my current Xcode project is being built for iPhone or iPad?

相关标签:
5条回答
  • 2021-02-04 12:53
    NSString *deviceType = [UIDevice currentDevice].model;
    
    if([deviceType isEqualToString:@"iPhone"]) {
        //iPhone
    }
    else if([deviceType isEqualToString:@"iPod touch"]) {
        //iPod Touch
    }
    else {
        //iPad
    }
    

    You cannot, as far as I am concerned, use #if or #ifdef to do this but, it is supported because Obj-C is a strict superset of C.

    Related: Determine device (iPhone, iPod Touch) with iPhone SDK

    0 讨论(0)
  • 2021-02-04 12:58

    Updation for swift:

    Couldn't use preprocessor. Make global function as

    func IS_IPAD() -> Bool { 
    ( return (UIDevice.respondsToSelector(Selector("userInterfaceIdiom"))) && (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad) )}
    
    0 讨论(0)
  • 2021-02-04 13:00

    Some ideas in the comment section of this blog

    http://greensopinion.blogspot.com/2010/04/from-iphone-to-ipad-creating-universal.html

    Mostly using

    UI_USER_INTERFACE_IDIOM()
    

    Such as:

    #ifdef UI_USER_INTERFACE_IDIOM()
      #define IS_IPAD() (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    #else
      #define IS_IPAD() (false)
    #endif
    
    0 讨论(0)
  • 2021-02-04 13:15

    There is no way to determine whether your app is built for iPhone or iPad. Preprocessor #if directives are resolved during build. Once your app is built and flagged as Universal, it has to run correctly on both devices. During building nobody knows where it will be installed later and one build can be installed on both.

    However you may want to do one of these:

    1. Detect device model during runtime.

      To do this, use [[UIDevice currentDevice] model] and compare to iPhone, iPod touch or iPad strings. This will return you correct device even when running in compatibility mode on iPad (for iPhone-only apps). This can be usefull for usage analytics.

    2. Detect user interface idiom during runtime.

      This is what everyone checks for, when providing different content for iPhone and iPad. Use [[UIDevice currentDevice] userInterfaceIdiom] and compare to UIUserInterfaceIdiomPhone or UIUserInterfaceIdiomPad. You may want to make convenience methods like this:

      @implementation UIDevice (UserInterfaceIdiom)
      
      - (BOOL)iPhone {
          return (self.userInterfaceIdiom == UIUserInterfaceIdiomPhone);
      }
      + (BOOL)iPhone {
          return [[UIDevice currentDevice] iPhone];
      }
      
      - (BOOL)iPad {
          return (self.userInterfaceIdiom == UIUserInterfaceIdiomPad);
      }
      + (BOOL)iPad {
          return [[UIDevice currentDevice] iPad];
      }
      
      @end
      

      Then you can use:

      if ([[UIDevice currentDevice] iPhone]) { }
      // or
      if ([UIDevice iPhone]) { }
      // or
      if (UIDevice.iPhone) { }
      
    0 讨论(0)
  • 2021-02-04 13:15

    I use the following code for AppleTV 4 because UIUserInterfaceIdiomUnspecified, doesn't seem to work, nor can I find any other enums:

    #ifdef TARGET_OS_IOS
    CGSize result = [[UIScreen mainScreen] bounds].size;
        if(result.width == 1920) {
            NSLog(@"tvOS");
        }
    #endif
    

    I used to use this for iPad and such before the dark times, before the empire- OB1, hah good night. But you can use this similar technique for other screen sizes you know of.

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