使用Swift中的UI_USER_INTERFACE_IDIOM()检测当前设备

Ber*_*lue 229 iphone objective-c ipad ios swift

UI_USER_INTERFACE_IDIOM()在Swift中,iPhone和iPad之间有什么相同的功能?

我得到一个Use of unresolved identifier在斯威夫特编译时错误.

Cez*_*zar 496

使用Swift时,您可以使用enum UIUserInterfaceIdiom,定义为:

enum UIUserInterfaceIdiom : Int {
    case unspecified

    case phone // iPhone and iPod touch style UI
    case pad // iPad style UI
}
Run Code Online (Sandbox Code Playgroud)

所以你可以用它作为:

UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified
Run Code Online (Sandbox Code Playgroud)

或者使用Switch语句:

    switch UIDevice.current.userInterfaceIdiom {
    case .phone:
        // It's an iPhone
    case .pad:
        // It's an iPad
    case .unspecified:
        // Uh, oh! What could it be?
    }
Run Code Online (Sandbox Code Playgroud)

UI_USER_INTERFACE_IDIOM() 是一个Objective-C宏,定义为:

#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)
Run Code Online (Sandbox Code Playgroud)

另请注意,即使使用Objective-C,UI_USER_INTERFACE_IDIOM()仅在定位iOS 3.2及更低版本时才需要宏.部署到iOS 3.2及更高版本时,您可以[UIDevice userInterfaceIdiom]直接使用.

  • 没关系.我得到它``如果UIDevice.currentDevice().userInterfaceIdiom == .Pad` (19认同)
  • 在Swift 3中,`UIDevice.currentDevice().userInterfaceIdiom`变为`UIDevice.current.userInterfaceIdiom` (5认同)
  • 正如Tony在下面的一个答案中提到的,当通过TestFlight部署应用程序时,Swift应用程序中的UI_USER_INTERFACE_IDIOM崩溃.奇怪的是,当应用程序从X-Code直接上传到设备时,它可以正常工作.我也遇到了这个bug. (4认同)
  • 如果您的应用程序仅适用于 iPhone,则此方法将无法正常工作,您将始终获得 .phone,请查看 Ricardo 的回答。 (2认同)

Bes*_*rov 112

您应该使用此GBDeviceInfo框架或...

Apple定义了这个:

public enum UIUserInterfaceIdiom : Int {

    case unspecified

    case phone // iPhone and iPod touch style UI

    case pad // iPad style UI

    @available(iOS 9.0, *)
    case tv // Apple TV style UI

    @available(iOS 9.0, *)
    case carPlay // CarPlay style UI
}
Run Code Online (Sandbox Code Playgroud)

所以对于设备的严格定义可以使用此代码

struct ScreenSize
{
    static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
    static let IS_IPHONE_4_OR_LESS  = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6_7          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P_7P         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
    static let IS_IPAD_PRO          = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}
Run Code Online (Sandbox Code Playgroud)

如何使用

if DeviceType.IS_IPHONE_6P_7P {
    print("IS_IPHONE_6P_7P")
}
Run Code Online (Sandbox Code Playgroud)

检测iOS版本

struct Version{
    static let SYS_VERSION_FLOAT = (UIDevice.current.systemVersion as NSString).floatValue
    static let iOS7 = (Version.SYS_VERSION_FLOAT < 8.0 && Version.SYS_VERSION_FLOAT >= 7.0)
    static let iOS8 = (Version.SYS_VERSION_FLOAT >= 8.0 && Version.SYS_VERSION_FLOAT < 9.0)
    static let iOS9 = (Version.SYS_VERSION_FLOAT >= 9.0 && Version.SYS_VERSION_FLOAT < 10.0)
}
Run Code Online (Sandbox Code Playgroud)

如何使用

if Version.iOS8 {
    print("iOS8")
}
Run Code Online (Sandbox Code Playgroud)

  • 我喜欢 `struct ScreenSize/DeviceType` 方法,因为它适用于模拟器 (2认同)

use*_*170 35

Swift 2.0和iOS 9和Xcode 7.1

// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.mainScreen().traitCollection.userInterfaceIdiom

// 2. check the idiom
switch (deviceIdiom) {

case .Pad:
    print("iPad style UI")
case .Phone:
    print("iPhone and iPod touch style UI")
case .TV: 
    print("tvOS style UI")
default:
    print("Unspecified UI idiom")

}
Run Code Online (Sandbox Code Playgroud)

Swift 3.0和Swift 4.0

// 1. request an UITraitCollection instance
let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom

// 2. check the idiom
switch (deviceIdiom) {

case .pad:
    print("iPad style UI")
case .phone:
    print("iPhone and iPod touch style UI")
case .tv: 
    print("tvOS style UI")
default:
    print("Unspecified UI idiom")
}
Run Code Online (Sandbox Code Playgroud)

使用UITraitCollection.iOS特征环境通过UITraitEnvironment协议的traitCollection属性公开.该协议由以下类采用:

  • UIScreen
  • 的UIWindow
  • 的UIViewController
  • UIPresentationController
  • 的UIView


Mas*_*ego 22

if/else case:

 if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)     
 {
        // Ipad
 }
 else 
 {
       // Iphone
 }
Run Code Online (Sandbox Code Playgroud)


ric*_*rdo 19

我这样做:

UIDevice.current.model
Run Code Online (Sandbox Code Playgroud)

它显示了设备的名称.

要检查是iPad还是iPhone:

if ( UIDevice.current.model.range(of: "iPad") != nil){
    print("I AM IPAD")
} else {
    print("I AM IPHONE")
}
Run Code Online (Sandbox Code Playgroud)

  • 对我来说绝对是最好的解决方案.检查userInterfaceIdiom有一个问题:如果你的应用是iPhone只,但你的iPad应用程序推出,userInterfaceIdiom是== .Phone (5认同)

Bro*_*son 17

Swift 4.2 - 5.1 扩展

 public extension UIDevice {

    class var isPhone: Bool {
        return UIDevice.current.userInterfaceIdiom == .phone
    }

    class var isPad: Bool {
        return UIDevice.current.userInterfaceIdiom == .pad
    }

    class var isTV: Bool {
        return UIDevice.current.userInterfaceIdiom == .tv
    }

    class var isCarPlay: Bool {
        return UIDevice.current.userInterfaceIdiom == .carPlay
    }
}
Run Code Online (Sandbox Code Playgroud)

用法

if UIDevice.isPad {
   // Do something
}
Run Code Online (Sandbox Code Playgroud)


iBu*_*Bug 10

尝试添加这样的扩展程序:

    public extension UIDevice {

    var modelName: String {
        var systemInfo = utsname()
        uname(&systemInfo)
        let machineMirror = Mirror(reflecting: systemInfo.machine)
        let identifier = machineMirror.children.reduce("") { identifier, element in
            guard let value = element.value as? Int8 where value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

        switch identifier {
        case "iPod5,1":                                 return "iPod Touch 5"
        case "iPod7,1":                                 return "iPod Touch 6"
        case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
        case "iPhone4,1":                               return "iPhone 4s"
        case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
        case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
        case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
        case "iPhone7,2":                               return "iPhone 6"
        case "iPhone7,1":                               return "iPhone 6 Plus"
        case "iPhone8,1":                               return "iPhone 6s"
        case "iPhone8,2":                               return "iPhone 6s Plus"
        case "iPhone9,1", "iPhone9,3":                  return "iPhone 7"
        case "iPhone9,2", "iPhone9,4":                  return "iPhone 7 Plus"
        case "iPhone8,4":                               return "iPhone SE"
        case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
        case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
        case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
        case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
        case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
        case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
        case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
        case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
        case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
        case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
        case "AppleTV5,3":                              return "Apple TV"
        case "i386", "x86_64":                          return "Simulator"
        default:                                        return identifier
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是你将如何使用它:

let modelName = UIDevice.currentDevice().modelName
Run Code Online (Sandbox Code Playgroud)

编辑 对于模拟器,您可以在这里尝试解决方案


Ale*_*ano 9

Swift 2.x:

添加到Beslav Turalov的答案是新的入门iPad Pro可以很容易地找到这条线

检测iPad Pro

struct DeviceType
{
    ...
    static let IS_IPAD_PRO = UIDevice.currentDevice().userInterfaceIdiom == .Pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
}
Run Code Online (Sandbox Code Playgroud)

Swift 3(增加电视和汽车):

struct ScreenSize
{
    static let SCREEN_WIDTH         = UIScreen.main.bounds.size.width
    static let SCREEN_HEIGHT        = UIScreen.main.bounds.size.height
    static let SCREEN_MAX_LENGTH    = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
    static let SCREEN_MIN_LENGTH    = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
}

struct DeviceType
{
    static let IS_IPHONE            = UIDevice.current.userInterfaceIdiom == .phone
    static let IS_IPHONE_4_OR_LESS  = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0
    static let IS_IPHONE_5          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0
    static let IS_IPHONE_6          = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0
    static let IS_IPHONE_6P         = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0
    static let IS_IPHONE_7          = IS_IPHONE_6
    static let IS_IPHONE_7P         = IS_IPHONE_6P
    static let IS_IPAD              = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0
    static let IS_IPAD_PRO_9_7      = IS_IPAD
    static let IS_IPAD_PRO_12_9     = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0
    static let IS_TV                = UIDevice.current.userInterfaceIdiom == .tv
    static let IS_CAR_PLAY          = UIDevice.current.userInterfaceIdiom == .carPlay
}

struct Version{
    static let SYS_VERSION_FLOAT = (UIDevice.current.systemVersion as NSString).floatValue
    static let iOS7 = (Version.SYS_VERSION_FLOAT < 8.0 && Version.SYS_VERSION_FLOAT >= 7.0)
    static let iOS8 = (Version.SYS_VERSION_FLOAT >= 8.0 && Version.SYS_VERSION_FLOAT < 9.0)
    static let iOS9 = (Version.SYS_VERSION_FLOAT >= 9.0 && Version.SYS_VERSION_FLOAT < 10.0)
    static let iOS10 = (Version.SYS_VERSION_FLOAT >= 10.0 && Version.SYS_VERSION_FLOAT < 11.0)
}
Run Code Online (Sandbox Code Playgroud)

用法:

if DeviceType.IS_IPHONE_7P { print("iPhone 7 plus") }
if DeviceType.IS_IPAD_PRO_9_7 && Version.iOS10 { print("iPad pro 9.7 with iOS 10 version") }
Run Code Online (Sandbox Code Playgroud)


小智 6

试试这个来检查当前设备是 iPhone 还是 iPad:

斯威夫特 5

struct Device {
    static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
    static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
}
Run Code Online (Sandbox Code Playgroud)

用:

if(Device.IS_IPHONE){
    // device is iPhone
}if(Device.IS_IPAD){
    // device is iPad (or a Mac running under macOS Catalyst)
}else{
    // other
}
Run Code Online (Sandbox Code Playgroud)


Abd*_*mer 6

自 iOS 13 起UI_USER_INTERFACE_IDIOM已被弃用。如果您的代码仍在 中Obj-C,您可以使用以下内容:

if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // device is iPad
}
Run Code Online (Sandbox Code Playgroud)

在哪里:

typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
    UIUserInterfaceIdiomUnspecified = -1,
    UIUserInterfaceIdiomPhone API_AVAILABLE(ios(3.2)), // iPhone and iPod touch style UI
    UIUserInterfaceIdiomPad API_AVAILABLE(ios(3.2)), // iPad style UI
    UIUserInterfaceIdiomTV API_AVAILABLE(ios(9.0)), // Apple TV style UI
    UIUserInterfaceIdiomCarPlay API_AVAILABLE(ios(9.0)), // CarPlay style UI
};
Run Code Online (Sandbox Code Playgroud)

  • 感谢您的 Obj-C 回答。 (3认同)

raa*_*aaz 5

在swift 4和Xcode 9.2中,您可以通过以下方式检测设备是否为iPhone/iPad.

if (UIDevice.current.userInterfaceIdiom == .pad){
   print("iPad")
}
else{
   print("iPhone")
}
Run Code Online (Sandbox Code Playgroud)

其他方式

    let deviceName = UIDevice.current.model
    print(deviceName);
    if deviceName == "iPhone"{
        print("iPhone")
    }
    else{
        print("iPad")
    }
Run Code Online (Sandbox Code Playgroud)