if (device == iPad), if (device == iPhone)

25,616

Solution 1

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
     // The device is an iPad running iOS 3.2 or later.
}
else
{
     // The device is an iPhone or iPod touch.
}

Solution 2

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

and

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

The macros UI_USER_INTERFACE_IDIOM() also works on older iOS versions like iOS 3.0 without crashing.

Solution 3

UI_USER_INTERFACE_IDIOM() is the best solution in your case since your app is universal. But if you are running an iPhone app on iPad than UI_USER_INTERFACE_IDIOM() will return UIUserInterfaceIdiomPhone, regardless of the device. For such purposes as that, you can use the UIDevice.model property:

if ([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound) {

    //Device is iPad
}

Solution 4

In Swift 2.x you can use the following equalities to determine the kind of device:

UIDevice.currentDevice().userInterfaceIdiom == .Phone

or

UIDevice.currentDevice().userInterfaceIdiom == .Pad

In Swift 3 for new people coming here.

if UIDevice.current.userInterfaceIdiom == .pad { \\ Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified \\ Implement your awesome logic here }

Solution 5

The UIDevice class will tell you everything you need to know about the device. The model property, for instance, will tell you the model of the device. You can use this to determine which view to use for the current device.

Share:
25,616
Peter Kazazes
Author by

Peter Kazazes

I'm a full stack platform developer, public speaker, and strategist. Beginning with iOS development as teenager in my bedroom (and much help from SO), I built the first social-media-sourced polling platform, Twelect, which I transformed into Sibyl Vision, a predict-anything public opinion analysis firm. I regularly speak about Sibyl's research at universities and on national television. These days, I build real-time data abstraction platforms which spotlight actionable business intelligence. I've built software for some of the country's largest institutions, and am obsessed with evolving digital policy and information security.

Updated on July 08, 2020

Comments

  • Peter Kazazes
    Peter Kazazes almost 4 years

    So I have a universal app and I'm setting up the content size of a UIScrollView. Obviously the content size is going to be different on iPhones and iPads. How can I set a certain size for iPads and another size for iPhones and iPod touches?

  • Constantino Tsarouhas
    Constantino Tsarouhas over 12 years
    It's not recommended to use the model property as it's not future-proof.
  • csano
    csano over 12 years
    That's the first I've heard of this. Can you provide me with a link? If this is the case, I'll remove my answer.
  • Constantino Tsarouhas
    Constantino Tsarouhas over 12 years
    Actually, I heard it on a WWDC session on making iOS apps future-proof. Using the model property gives you a string. If Apple releases a new iOS device that is similar to the iPhone but has another name, say "iPhone nano", the app won't be able to function correctly. I advise using UIScreen's applicationFrame property for this situation. It's analogous to determining hardware capabilities: instead of only enabling the gyroscope on the iPhone and the iPad, it's better to enable it only if Core Motion says so. If once the iPod touch or a future device also supports it, you've got a problem. ;-)
  • FeifanZ
    FeifanZ over 12 years
    Idioms are the way they are now, and it's trivial to change it in the future. They're a lot more clear and meaningful than your suggestion, IMHO.
  • Constantino Tsarouhas
    Constantino Tsarouhas over 12 years
    @Inspire48: The question is: how can I properly size my scroll view? The easiest way to do that is by using the size the system gives. So, even on a iDevice not yet known, the code wouldn't break, because you wouldn't care about the device (idioms), but only about the display (sizing a scroll view, etc.). Obviously, for many other circumstances, using the idiom is the way to go. ;-)
  • KennethJ
    KennethJ over 9 years
    This is not realiable. As far as I'm aware, the UIDevice.model property is equal to the string present in Settings > About and is changable by the user. Source: developer.apple.com/library/ios/documentation/uikit/referenc‌​e/…
  • Yunus Nedim Mehel
    Yunus Nedim Mehel over 9 years
    @KennethJ thanks for the warning but I have checked it and I don't think so. Settings > general > about > Model gives "MD297B/A" for my iPhone 5 and is not editable (on the menu only the field "name" is editable). The API link you gave states that device.model is readonly and @”iPhone” and @”iPod touch” are possible values.
  • KennethJ
    KennethJ over 9 years
    Yes you are quite right, I was mistakenly looking at "name" rather than "model". I stand corrected.