iOS detect if user is on an iPad

162,616

Solution 1

There are quite a few ways to check if a device is an iPad. This is my favorite way to check whether the device is in fact an iPad:

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    return YES; /* Device is iPad */
}

The way I use it

#define IDIOM    UI_USER_INTERFACE_IDIOM()
#define IPAD     UIUserInterfaceIdiomPad

if ( IDIOM == IPAD ) {
    /* do something specifically for iPad. */
} else {
    /* do something specifically for iPhone or iPod touch. */
}   

Other Examples

if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) {
    return YES; /* Device is iPad */
}

#define IPAD     (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
if ( IPAD ) 
     return YES;

For a Swift solution, see this answer: https://stackoverflow.com/a/27517536/2057171

Solution 2

In Swift you can use the following equalities to determine the kind of device on Universal apps:

UIDevice.current.userInterfaceIdiom == .phone
// or
UIDevice.current.userInterfaceIdiom == .pad

Usage would then be something like:

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

Solution 3

This is part of UIDevice as of iOS 3.2, e.g.:

[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad

Solution 4

You can also use this

#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
   // iPad
} else {
   // iPhone / iPod Touch
}

Solution 5

UI_USER_INTERFACE_IDIOM() only returns iPad if the app is for iPad or Universal. If its an iPhone app running on an iPad then it won't. So you should instead check the model.

Share:
162,616

Related videos on Youtube

Albert Renshaw
Author by

Albert Renshaw

CEO, Apps4Life, LLC.

Updated on July 18, 2022

Comments

  • Albert Renshaw
    Albert Renshaw almost 2 years

    I have an app that runs on the iPhone and iPod Touch, it can run on the Retina iPad and everything but there needs to be one adjustment. I need to detect if the current device is an iPad. What code can I use to detect if the user is using an iPad in my UIViewController and then change something accordingly?

  • Albert Renshaw
    Albert Renshaw over 11 years
    Maybe Apple updated the simulator to say something like "iPad simulator" or "iPad 2.1" or something... if that's the case you could use hasSuffix:@"iPad" instead of isEqualToString@"iPad"... your best bet is to Log the device model that simulator does return and go from there...
  • Alex S
    Alex S about 11 years
    The way you use it isn't as efficient as it could be. UI_USER_INTERFACE_IDIOM() is equivalent to ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? [[UIDevice currentDevice] userInterfaceIdiom] : UIUserInterfaceIdiomPhone). You might be better off caching the result somewhere: BOOL iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad; … if (iPad) ….
  • elbuild
    elbuild over 10 years
    I would use hasPrefix rather than isEqualToString in your last method. In this way the code works on the simulator as well.
  • Yunus Nedim Mehel
    Yunus Nedim Mehel over 10 years
    idiom is generally better but if you are running an iphone app on iPad than this will return UIUserInterfaceIdiomPhone.
  • WrightsCS
    WrightsCS about 10 years
    Yes this will return iPad for the mini.
  • Pang
    Pang almost 10 years
    Swift: if UIDevice.currentDevice().userInterfaceIdiom == .Pad
  • Albert Renshaw
    Albert Renshaw over 9 years
    I'm Editing a link to your answer into the accepted answer. (This way you get credit too). Even though this is an objective-c question, alot of people viewing this question are coming from Google and may be searching for a Swift solution! :D
  • Jeehut
    Jeehut over 9 years
    Thank you, @AlbertRenshaw. I thought so, too. :) Btw: I don't think the intention of the question was to ask specifically for Objective-C, but for iOS (which was Obj-C at that moment). At least I would have expected to find the answer under this question for Swift as well.
  • Ky -
    Ky - about 9 years
    #ifdef UI_USER_INTERFACE_IDIOM is never entered for me, using Xcode 6.3 compiling for iOS 8.2
  • Christian Schnorr
    Christian Schnorr almost 9 years
    I don't think there's a need to rewrite old answers to questions tagged Objective-C to swift.
  • King-Wizard
    King-Wizard almost 9 years
    I definitely think that my answer is useful because firstly all answers are scattered on stack overflow. Secondly what worked with older versions of iOS does not correctly work sometimes with iOS 8 and above. So I tested these solutions and this answer can be highly useful. So I do not agree with you at all.
  • King-Wizard
    King-Wizard almost 9 years
    On top of that the syntax is different in Swift. So it is always useful for everybody to do an intelligent copy paste of the answer and understanding the specific up-to-date nuts and bolts.
  • Chris Hayes
    Chris Hayes almost 9 years
  • Jeehut
    Jeehut over 8 years
    Hi @sevensevens, thank you for your feedback. I just tried this out and it did work for me in XCode 7.2 targeting iOS 9 in the simulator. What version of XCode are you using? Maybe it doesn't work on older XCodes? The docs say userInterfaceIdiom is 'Available in iOS 3.2 and later.' so that shouldn't be the problem.
  • Jeehut
    Jeehut over 8 years
    Or could it be that you are running an iPhone only app on the iPad simulator? In that case, that would explain the confusion – but it should also behave this way on real devices, I think. As @Yunus Nedim Mehel points out in the comments of @Richards answer that situation will return .Phone instead of .Pad.
  • sevensevens
    sevensevens over 8 years
    Sorry - had the simulator set to iPhone. Gotta stop making changes at 2AM
  • Oliver
    Oliver almost 7 years
    Does this also work when the iPad-App is in Split Screen Mode? Then the horizontal size class would be compact.
  • Albert Renshaw
    Albert Renshaw over 6 years
    Note: This question was asked in 2012
  • gnasher729
    gnasher729 over 6 years
    Godawful use of macros. Great way to obfuscate your code.
  • WrightsCS
    WrightsCS over 6 years
    @gnasher729 500+ people tend to disagree with you. Instead of the snarky comments, why don't you provide your own answer since you think you have a better way to do this.
  • Cœur
    Cœur over 6 years
    ["I am not an iPad" rangeOfString:@"iPad"].location != NSNotFound returns true.
  • Cœur
    Cœur over 6 years
    Why bothering with an App struct when you can do the same with an UIDevice extension?
  • Graham Perks
    Graham Perks about 5 years
    Swift 4: if UIDevice.current.userInterfaceIdiom == .pad
  • Viktor Sehr
    Viktor Sehr about 4 years
    WrightsCS: You already provided a better way right above the macro way...
  • Albert Renshaw
    Albert Renshaw over 3 years
    What an oversight by apple! I wonder if it’s a bug? Thanks for sharing
  • iaomw
    iaomw almost 3 years
    Does it happen during compile time or run time?