iOS detect if user is on an iPad
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.
Related videos on Youtube
Comments
-
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 over 11 yearsMaybe 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 ofisEqualToString@"iPad"
... your best bet is to Log the device model that simulator does return and go from there... -
Alex S about 11 yearsThe 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 over 10 yearsI would use hasPrefix rather than isEqualToString in your last method. In this way the code works on the simulator as well.
-
Yunus Nedim Mehel over 10 yearsidiom is generally better but if you are running an iphone app on iPad than this will return UIUserInterfaceIdiomPhone.
-
WrightsCS about 10 yearsYes this will return iPad for the mini.
-
Pang almost 10 yearsSwift:
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
-
Albert Renshaw over 9 yearsI'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 over 9 yearsThank 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 - about 9 years
#ifdef UI_USER_INTERFACE_IDIOM
is never entered for me, using Xcode 6.3 compiling for iOS 8.2 -
Christian Schnorr almost 9 yearsI don't think there's a need to rewrite old answers to questions tagged Objective-C to swift.
-
King-Wizard almost 9 yearsI 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 almost 9 yearsOn 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 almost 9 years
-
Jeehut over 8 yearsHi @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 over 8 yearsOr 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 over 8 yearsSorry - had the simulator set to iPhone. Gotta stop making changes at 2AM
-
Oliver almost 7 yearsDoes this also work when the iPad-App is in Split Screen Mode? Then the horizontal size class would be compact.
-
Albert Renshaw over 6 yearsNote: This question was asked in 2012
-
gnasher729 over 6 yearsGodawful use of macros. Great way to obfuscate your code.
-
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 over 6 years
["I am not an iPad" rangeOfString:@"iPad"].location != NSNotFound
returns true. -
Cœur over 6 yearsWhy bothering with an
App
struct when you can do the same with anUIDevice
extension? -
Graham Perks about 5 yearsSwift 4:
if UIDevice.current.userInterfaceIdiom == .pad
-
Viktor Sehr about 4 yearsWrightsCS: You already provided a better way right above the macro way...
-
Albert Renshaw over 3 yearsWhat an oversight by apple! I wonder if it’s a bug? Thanks for sharing
-
iaomw almost 3 yearsDoes it happen during compile time or run time?