Best way to programmatically detect iPad/iPhone hardware

前端 未结 10 868
借酒劲吻你
借酒劲吻你 2020-11-27 14:14

The reason I need to find out is that on an iPad, a UIPickerView has the same height in landscape orientation as it does in portrait. On an iPhone it is different. The iPad

相关标签:
10条回答
  • 2020-11-27 14:39

    Put this method in your App Delegate so that you can call it anywhere using [[[UIApplication sharedApplication] delegate] isPad]

    -(BOOL)isPad
    {
        BOOL isPad;
        NSRange range = [[[UIDevice currentDevice] model] rangeOfString:@"iPad"];
        if(range.location==NSNotFound)
        {
            isPad=NO;
    
    
        }
        else {
            isPad=YES;
        }
    
        return isPad;
    }
    
    0 讨论(0)
  • 2020-11-27 14:42
    BOOL isIpad()
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            return YES;
        }
        return NO;
    }
    
    0 讨论(0)
  • 2020-11-27 14:43

    I like my isPad() function. Same code but keep it out of sight and in only one place.

    0 讨论(0)
  • 2020-11-27 14:43

    In Swift use userInterfaceIdiom instance property as-

    if UIDevice.current.userInterfaceIdiom == .phone {
         print("iPhone")
     }
    

    & For other devices -

      switch UIDevice.current.userInterfaceIdiom {
        case .pad:
            print("iPad")
        case .phone:
            print("iPhone")
        case .tv:
            print("TV")
        case .carPlay:
            print("carPlay")
        default: break;
      }
    
    0 讨论(0)
  • 2020-11-27 14:43
    extension UIDevice {
    
       var isIPad: Bool {
          return UIDevice.current.userInterfaceIdiom == .pad
       }
    }
    
    0 讨论(0)
  • 2020-11-27 14:48

    I'm answering this now (and at this late date) because many of the existing answers are quite old, and the most Up Voted actually appears to be wrong according to Apples current docs (iOS 8.1, 2015)!

    To prove my point, this is the comment from Apples header file (always look at the Apple source and headers):

    /*The UI_USER_INTERFACE_IDIOM() macro is provided for use when
      deploying to a version of the iOS less than 3.2. If the earliest
      version of iPhone/iOS that you will be deploying for is 3.2 or
      greater, you may use -[UIDevice userInterfaceIdiom] directly.*/
    

    Therefore, the currently APPLE recommended way to detect iPhone vs. iPad, is as follows:

    1) (DEPRECATED as of iOS 13) On versions of iOS PRIOR to 3.2, use the Apple provided macro:

    // for iPhone use UIUserInterfaceIdiomPhone
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    

    2) On versions of iOS 3.2 or later, use the property on [UIDevice currentDevice]:

    // for iPhone use UIUserInterfaceIdiomPhone
    if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    
    0 讨论(0)
提交回复
热议问题