iOS检测用户是否在iPad上
我有一个在iPhone和iPod Touch上运行的应用程序,它可以在Retina iPad上运行,除此之外,还有一些需要进行调整。 我需要检测当前设备是否是iPad。 我可以使用哪些代码来检测用户是否在我的UIViewController使用iPad,然后相应地更改相应内容? 
有很多方法可以检查设备是否是iPad。 这是我最喜欢的方式来检查设备是否实际上是一个iPad:
 if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) { return YES; /* Device is iPad */ } 
我用它的方式
 #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. */ } 
其他例子
 if ( [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"] ) { return YES; /* Device is iPad */ } #define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad if ( IPAD ) return YES; 
对于Swift解决scheme,请参阅此答案: https : //stackoverflow.com/a/27517536/2057171
在Swift中 ,可以使用下面的等式来确定设备的种类 :
 UIDevice.current.userInterfaceIdiom == .phone // or UIDevice.current.userInterfaceIdiom == .pad 
用法将会是这样的:
 if UIDevice.current.userInterfaceIdiom == .pad { // Available Idioms - .pad, .phone, .tv, .carPlay, .unspecified // Implement your logic here } 
这是iOS 3.2的UIDevice的一部分,例如:
 [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad 
你也可以使用这个
 #define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ... if (IPAD) { // iPad } else { // iPhone / iPod Touch } 
UI_USER_INTERFACE_IDIOM()仅在iPad或通用应用程序的情况下返回iPad。 如果它的iPhone应用程序在iPad上运行,那么它不会。 所以你应该检查模型。
我发现这在Xcode 4.5中的模拟器中不适用于我
 NSString *deviceModel = (NSString*)[UIDevice currentDevice].model; if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) { DebugLog(@"iPad"); } else { DebugLog(@"iPhone or iPod Touch"); } 
另外在Xcode中的“其他例子”中,设备模型会以“iPad模拟器”的forms出现,因此上述调整应该将其排除。
请注意:如果您的应用仅针对iPhone设备,则使用iphone兼容模式运行的iPad将针对以下语句返回false:
 #define IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad 
检测物理iPad设备的正确方法是:
 #define IS_IPAD_DEVICE ([(NSString *)[UIDevice currentDevice].model hasPrefix:@"iPad"]) 
Swift中有很多方法可以做到这一点:
我们检查下面的模型(我们只能在这里做一个区分大小写的search):
 class func isUserUsingAnIpad() -> Bool { let deviceModel = UIDevice.currentDevice().model let result: Bool = NSString(string: deviceModel).containsString("iPad") return result } 
我们检查下面的模型(我们可以在这里做一个区分大小写/不敏感的search):
  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下面的UIDevice.currentDevice().userInterfaceIdiom只返回iPad,如果应用程序是iPad或通用。 如果这是一个iPhone应用程序正在iPad上运行,那么它不会。 所以你应该检查模型。  : 
  class func isUserUsingAnIpad() -> Bool { if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad { return true } else { return false } } 
 如果这个类没有inheritanceUIViewController ,那么下面的代码片段不会编译,否则它就可以正常工作。 无论如何, UI_USER_INTERFACE_IDIOM()只会返回iPad,如果应用程序是iPad或通用。 如果这是一个iPhone应用程序正在iPad上运行,那么它不会。 所以你应该检查模型。  : 
 class func isUserUsingAnIpad() -> Bool { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.Pad) { return true } else { return false } } 
*
在迅速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 } 
你可以检查rangeOfString来查看iPad这个词的存在。
 NSString *deviceModel = (NSString*)[UIDevice currentDevice].model; if ([deviceModel rangeOfString:@"iPad"].location != NSNotFound) { NSLog(@"I am an iPad"); } else { NSLog(@"I am not an iPad"); } 
又一个Swifty的方式:
 //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 } } 
用法:
 if UIDevice.type == iPhone { //it's an iPhone! } if UIDevice.type == iPad { //it's an iPad! } if UIDevice.type == TV { //it's an TV! } 
 对于最新版本的iOS,只需添加UITraitCollection : 
 extension UITraitCollection { var isIpad: Bool { return horizontalSizeClass == .regular && verticalSizeClass == .regular } } 
 然后在UIViewController检查: 
 if traitCollection.isIpad { ... } 
 if(UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.pad) { print("This is iPad") }else if (UI_USER_INTERFACE_IDIOM () == UIUserInterfaceIdiom.phone) { print("This is iPhone"); } 
为什么这么复杂? 我就是这么做的
Swift 4:
 var iPad : Bool { return UIDevice.current.model.contains("iPad") } 
 这样,你可以说, if iPad {} 
iPhoneX返回成语iPad (至less在模拟器中,实际的设备将在几天后发售),即使它是iPhone。 因此,正确的答案是使用设备名称。
Swift 4:
 var iPad : Bool { return UIDevice.current.model.contains("iPad") } 
OBJ-C:
 - (BOOL)iPad { return ([deviceModel rangeOfString:@"iPad"].location != NSNotFound }