问题
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?
回答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
回答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
}
回答3:
This is part of UIDevice as of iOS 3.2, e.g.:
[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad
回答4:
You can also use this
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
...
if (IPAD) {
// iPad
} else {
// iPhone / iPod Touch
}
回答5:
UI_USER_INTERFACE_IDIOM() only returns iPad if the app is for iPad or Universal. If its an iPhone app being ran on an iPad then it won't. So you should instead check the model.
回答6:
Be Careful: If your app is targeting iPhone device only, iPad running with iphone compatible mode will return false for below statement:
#define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
The right way to detect physical iPad device is:
#define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"])
回答7:
I found that some solution didn't work for me in the Simulator within Xcode. Instead, this works:
ObjC
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
DebugLog(@"iPad");
} else {
DebugLog(@"iPhone or iPod Touch");
}
Swift
if UIDevice.current.model.hasPrefix("iPad") {
print("iPad")
} else {
print("iPhone or iPod Touch")
}
Also in the 'Other Examples' in Xcode the device model comes back as 'iPad Simulator' so the above tweak should sort that out.
回答8:
Many ways to do that in Swift:
We check the model below (we can only do a case sensitive search here):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let result: Bool = NSString(string: deviceModel).containsString("iPad")
return result
}
We check the model below (we can do a case sensitive/insensitive search here):
class func isUserUsingAnIpad() -> Bool {
let deviceModel = UIDevice.currentDevice().model
let deviceModelNumberOfCharacters: Int = count(deviceModel)
if deviceModel.rangeOfString("iPad",
options: NSStringCompareOptions.LiteralSearch,
range: Range<String.Index>(start: deviceModel.startIndex,
end: advance(deviceModel.startIndex, deviceModelNumberOfCharacters)),
locale: nil) != nil {
return true
} else {
return false
}
}
UIDevice.currentDevice().userInterfaceIdiom
below only returns iPad if the app is for iPad or Universal. If it is an iPhone app being ran on an iPad then it won't. So you should instead check the model. :
class func isUserUsingAnIpad() -> Bool {
if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad {
return true
} else {
return false
}
}
This snippet below does not compile if the class does not inherit of an UIViewController
, otherwise it works just fine. Regardless UI_USER_INTERFACE_IDIOM()
only returns iPad if the app is for iPad or Universal. If it is an iPhone app being ran on an iPad then it won't. So you should instead check the model. :
class func isUserUsingAnIpad() -> Bool {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) {
return true
} else {
return false
}
}
回答9:
*
In swift 3.0
*
if UIDevice.current.userInterfaceIdiom == .pad {
//pad
} else if UIDevice.current.userInterfaceIdiom == .phone {
//phone
} else if UIDevice.current.userInterfaceIdiom == .tv {
//tv
} else if UIDevice.current.userInterfaceIdiom == .carPlay {
//CarDisplay
} else {
//unspecified
}
回答10:
Many Answers are good but I use like this in swift 4
Create Constant
struct App { static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false }
Use like this
if App.isRunningOnIpad { return load(from: .main, identifier: identifier) } else { return load(from: .ipad, identifier: identifier) }
Edit: As Suggested Cœur simply create an extension on UIDevice
extension UIDevice {
static let isRunningOnIpad = UIDevice.current.userInterfaceIdiom == .pad ? true : false
}
回答11:
You can check the rangeOfString to see of the word iPad exists like this.
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound) {
NSLog(@"I am an iPad");
} else {
NSLog(@"I am not an iPad");
}
回答12:
Yet another Swifty way:
//MARK: - Device Check
let iPad = UIUserInterfaceIdiom.Pad
let iPhone = UIUserInterfaceIdiom.Phone
@available(iOS 9.0, *) /* AppleTV check is iOS9+ */
let TV = UIUserInterfaceIdiom.TV
extension UIDevice {
static var type: UIUserInterfaceIdiom
{ return UIDevice.currentDevice().userInterfaceIdiom }
}
Usage:
if UIDevice.type == iPhone {
//it's an iPhone!
}
if UIDevice.type == iPad {
//it's an iPad!
}
if UIDevice.type == TV {
//it's an TV!
}
回答13:
In Swift 4.2 and Xcode 10
if UIDevice().userInterfaceIdiom == .phone {
//This is iPhone
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
} else if UIDevice().userInterfaceIdiom == .tv {
//This is Apple TV
}
If you want to detect specific device
let screenHeight = UIScreen.main.bounds.size.height
if UIDevice().userInterfaceIdiom == .phone {
if (screenHeight >= 667) {
print("iPhone 6 and later")
} else if (screenHeight == 568) {
print("SE, 5C, 5S")
} else if(screenHeight<=480){
print("4S")
}
} else if UIDevice().userInterfaceIdiom == .pad {
//This is iPad
}
回答14:
Why so complicated? This is how I do it...
Swift 4:
var iPad : Bool {
return UIDevice.current.model.contains("iPad")
}
This way you can just say if iPad {}
回答15:
For the latest versions of iOS, simply add UITraitCollection
:
extension UITraitCollection {
var isIpad: Bool {
return horizontalSizeClass == .regular && verticalSizeClass == .regular
}
}
and then within UIViewController
just check:
if traitCollection.isIpad { ... }
回答16:
if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.pad)
{
print("This is iPad")
}else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.phone)
{
print("This is iPhone");
}
来源:https://stackoverflow.com/questions/10167221/ios-detect-if-user-is-on-an-ipad