什么是等价的UI_USER_INTERFACE_IDIOM()在Swift检测之间的iPhone和iPad?

我得到一个使用未解决的标识符错误时,在Swift编译。


当前回答

我是这样做的:

UIDevice.current.model

它显示了设备的名称。

检查是iPad还是iPhone:

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

其他回答

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

enum UIUserInterfaceIdiom : Int {
    case unspecified
    
    case phone // iPhone and iPod touch style UI
    case pad   // iPad style UI (also includes macOS Catalyst)
}

所以你可以这样使用它:

UIDevice.current.userInterfaceIdiom == .pad
UIDevice.current.userInterfaceIdiom == .phone
UIDevice.current.userInterfaceIdiom == .unspecified

或者使用Switch语句:

    switch UIDevice.current.userInterfaceIdiom {
    case .phone:
        // It's an iPhone
    case .pad:
        // It's an iPad (or macOS Catalyst)

     @unknown default:
        // Uh, oh! What could it be?
    }

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

#define UI_USER_INTERFACE_IDIOM() \ ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \ [[UIDevice currentDevice] userInterfaceIdiom] : \ UIUserInterfaceIdiomPhone)

还要注意,即使在使用Objective-C时,UI_USER_INTERFACE_IDIOM()宏也只在针对iOS 3.2及以下时才需要。当部署到iOS 3.2及以上版本时,可以直接使用[UIDevice userInterfaceIdiom]。

如果寇/换:

 if UIDevice.current.userInterfaceIdiom == .pad {
     // iPad
 } else {
     // not iPad (iPhone, mac, tv, carPlay, unspecified)
 }

如果你想检查当前设备是iPad还是iPhone,那么你可以使用这些代码行:

 if(UIDevice.currentDevice().userInterfaceIdiom == .Pad){

  }else if(UIDevice.currentDevice().userInterfaceIdiom == .Phone){

  }

尝试添加这样的扩展:

    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
        }
    }

}

下面是你如何使用它:

let modelName = UIDevice.currentDevice().modelName

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

你可以在Swift 5上使用新的方式:

switch traitCollection.userInterfaceIdiom {
        
    case .unspecified:
        // do something
    case .phone:
        // do something
    case .pad:
        // do something
    case .tv:
        // do something
    case .carPlay:
        // do something
    case .mac:
        // do something
    @unknown default:
        // do something
}