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


243

UI_USER_INTERFACE_IDIOM()在iPhone和iPad之间检测到的Swift 相当于什么?

Use of unresolved identifier在Swift中编译时出现错误。

Answers:


534

使用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)
    case .unspecified:
        // 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]直接使用。


19
没关系。我了解到if UIDevice.currentDevice().userInterfaceIdiom == .Pad
Mihai Fratu 2014年

4
正如Tony在以下答案之一中所述,通过TestFlight部署应用程序时,Swift应用程序中的UI_USER_INTERFACE_IDIOM崩溃。奇怪的是,当应用程序从X-Code直接上传到设备时,它可以工作。我也遇到了这个错误。
Zmey

1
@Zmey是的,我的应用也被拒绝了,因为UI_USER_INTERFACE_IDIOM在审查中崩溃,非常奇怪
Peacemoon 2015年

非常感谢。当我发布了新版本的应用程序并从App Store崩溃时,我为这个bug苦苦挣扎,但是从来没有通过XCode安装该错误。希望我能尽快得到修复的复审。
Computerspezl

6
在Swift 3中UIDevice.currentDevice().userInterfaceIdiom成为UIDevice.current.userInterfaceIdiom
西藏沿海地区

113

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

苹果对此进行了定义:

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
}

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

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
}

如何使用

if DeviceType.IS_IPHONE_6P_7P {
    print("IS_IPHONE_6P_7P")
}

检测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)
}

如何使用

if Version.iOS8 {
    print("iOS8")
}

1
我喜欢struct ScreenSize/DeviceType这种方法,因为它可以在Simulator中使用
SoftDesigner,2015年

批准的答案应该转到这个简洁的答案
Ashoor,2015年

来自印度的巨大爱心,感谢您的努力,非常感谢您的分享和使Stackoverflow变得更好;)
swiftBoy 2016年

好答案!从Zaur +1)
XXX

什么是更新的代码?在适用于iPhone 7和7P的DEVICE_TYPE的背景下
Vaibhav Jhaveri

36

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")

}

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

使用UITraitCollection。通过UITraitEnvironment协议的traitCollection属性公开了iOS trait环境。以下类别采用了此协议:

  • UIScreen
  • UIWindow
  • UIViewController
  • UIPresentationController
  • UIView

25

如果/其他情况:

 if (UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad)     
 {
        // Ipad
 }
 else 
 {
       // Iphone
 }

公共枚举UIUserInterfaceIdiom:Int {case未指定@available(iOS 3.2,*)case Phone // iPhone和iPod touch风格的UI @available(iOS 3.2,*)case Pad // iPad style UI @available(iOS 9.0,*)case电视// Apple TV样式的UI}检查UIUserInterfaceIdiom的定义。如果不是Pad,则可以是Phone,TV,Unspecified。
UnchartedWorks

19

我这样做:

UIDevice.current.model

它显示设备的名称。

要检查是iPad还是iPhone:

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

5
绝对是最好的解决方案,至少对我而言。检查userInterfaceIdiom是否有问题:如果您的应用程序仅适用于iPhone,但您在iPad上启动了该应用程序,则userInterfaceIdiom是== .Phone
Luca Davanzo

绝对是最佳解决方案。好一个
Fattie

10

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
}

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

用法

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

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

}

这是您将如何使用它:

let modelName = UIDevice.currentDevice().modelName

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


不错,尽管使用模拟器进行测试只会返回模拟器。有没有解决的办法?
史蒂夫(Steve)

7

Swift 4.2扩展

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

用法

if UIDevice.isPad {
   // Do something
}

5

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

if (UIDevice.current.userInterfaceIdiom == .pad){
   print("iPad")
}
else{
   print("iPhone")
}

其他方式

    let deviceName = UIDevice.current.model
    print(deviceName);
    if deviceName == "iPhone"{
        print("iPhone")
    }
    else{
        print("iPad")
    }

5

谢谢大家的支持:))

UIDevice + Extensions.swift

import Foundation
import UIKit

extension UIDevice {
    static let 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, value != 0 else { return identifier }
            return identifier + String(UnicodeScalar(UInt8(value)))
        }

         func mapToDevice(identifier: String) -> String { // swiftlint:disable:this cyclomatic_complexity
            #if os(iOS)
            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 "iPhone10,1", "iPhone10,4":                return "iPhone 8"
            case "iPhone10,2", "iPhone10,5":                return "iPhone 8 Plus"
            case "iPhone10,3", "iPhone10,6":                return "iPhone X"
            case "iPhone11,2":                              return "iPhone XS"
            case "iPhone11,4", "iPhone11,6":                return "iPhone XS Max"
            case "iPhone11,8":                              return "iPhone XR"
            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 "iPad6,11", "iPad6,12":                    return "iPad 5"
            case "iPad7,5", "iPad7,6":                      return "iPad 6"
            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":                      return "iPad Pro 9.7 Inch"
            case "iPad6,7", "iPad6,8":                      return "iPad Pro 12.9 Inch"
            case "iPad7,1", "iPad7,2":                      return "iPad Pro 12.9 Inch 2. Generation"
            case "iPad7,3", "iPad7,4":                      return "iPad Pro 10.5 Inch"
            case "AppleTV5,3":                              return "Apple TV"
            case "AppleTV6,2":                              return "Apple TV 4K"
            case "AudioAccessory1,1":                       return "HomePod"
            case "i386", "x86_64":                          return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS"))"
            default:                                        return identifier
            }
            #elseif os(tvOS)
            switch identifier {
            case "AppleTV5,3": return "Apple TV 4"
            case "AppleTV6,2": return "Apple TV 4K"
            case "i386", "x86_64": return "Simulator \(mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS"))"
            default: return identifier
            }
            #endif
        }
        return mapToDevice(identifier: identifier)
    }()
}

enum DeviceName: String {
    case iPod_Touch_5 = "iPod Touch 5"
    case pod_Touch_6 = "Pod Touch 6"
    case iPhone_4 = "iPhone 4"
    case iPhone_4s = "iPhone 4s"
    case iPhone_5 = "iPhone 5"
    case iPhone_5c = "iPhone 5c"
    case iPhone_5s = "iPhone 5s"
    case iPhone_6 = "iPhone 6"
    case iPhone_6_Plus = "iPhone 6 Plus"
    case iPhone_6s = "iPhone 6s"
    case iPhone_6s_Plus = "iPhone 6s Plus"
    case iPhone_7 = "iPhone 7"
    case iPhone_7_Plus = "iPhone 7 Plus"
    case iPhone_SE = "iPhone SE"
    case iPhone_8 = "iPhone 8"
    case iPhone_8_Plus = "iPhone 8 Plus"
    case iPhone_X = "iPhone X"
    case iPhone_XS = "iPhone XS"
    case iPhone_XS_Max = "iPhone XS Max"
    case iPhone_XR = "iPhone XR"
    case iPad_2 = "iPad 2"
    case iPad_3 = "iPad 3"
    case iPad_4 = "iPad 4"
    case iPad_Air = "iPad Air"
    case iPad_Air_2 = "iPad Air 2"
    case iPad_5 = "iPad 5"
    case iPad_6 = "iPad 6"
    case iPad_Mini = "iPad Mini"
    case iPad_Mini_2 = "iPad Mini 2"
    case iPad_Mini_3 = "iPad Mini 3"
    case iPad_Mini_4 = "iPad Mini 4"
    case iPad_Pro_9_7_Inch = "iPad Pro 9.7 Inch"
    case iPad_Pro_12_9_Inch = "iPad Pro 12.9 Inch"
    case iPad_Pro_12_9_Inch_2_Generation = "iPad Pro 12.9 Inch 2. Generation"
    case iPad_Pro_10_5_Inch = "iPad Pro 10.5 Inch"
    case apple_TV = "Apple TV"
    case apple_TV_4K = "Apple TV 4K"
    case homePod = "HomePod"
}

SharedFunctions.swift

import Foundation
import UIKit
func isDevice(_ name: DeviceName) -> Bool {
    let modelName = UIDevice.modelName.replacingOccurrences(of: "Simulator", with: "").trimmed()
    if name.rawValue == modelName {
        return true
    }

    return false
}

String + Whitespace.swift

import Foundation

extension String {
   public func trimmed() -> String {
    return self.trimmingCharacters(in: .whitespacesAndNewlines)
  }
}

5

尝试以下操作以检查当前设备是iPhone还是iPad:

迅捷5

struct Device {
    static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
    static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
}

用:

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

3

Swift 2.0和iOS 7 + / iOS 8 + / iOS 9+

public class Helper {
    public class var isIpad:Bool {
        if #available(iOS 8.0, *) {
            return UIScreen.mainScreen().traitCollection.userInterfaceIdiom == .Pad
        } else {
            return UIDevice.currentDevice().userInterfaceIdiom == .Pad
        }
    }
    public class var isIphone:Bool {
        if #available(iOS 8.0, *) {
            return UIScreen.mainScreen().traitCollection.userInterfaceIdiom == .Phone
        } else {
            return UIDevice.currentDevice().userInterfaceIdiom == .Phone
        }
    }
}

用 :

if Helper.isIpad {

}

要么

guard Helper.isIpad else {
    return
} 

谢谢@ user3378170


感谢@ user3378170 for iOS 9
YannSteph 2015年

3

仅供参考,我已经使用 UI_USER_INTERFACE_IDIOM()了我用Swift编写的应用程序。该应用程序可以使用XCode 6.3.1很好地编译,而无需对该命令发出任何警告,并且可以在Simulator(使用任何选定的设备)上以及在我的所有实际设备(iPhone,iPad)(iOS版本为7.1至8.3)上良好运行。

但是,该应用在苹果审阅者的设备上崩溃了(并被拒绝)。我花了几天的时间才发现问题,而又没有再将其重新上传到iTunes Connect。

现在我UIDevice.currentDevice().userInterfaceIdiom改用了,我的应用程序可以在这种崩溃中幸存下来。


完全正确。它确实崩溃了,让我很头疼,想找出问题所在。
Praveen

每当我在代码中使用UI_USER_INTERFACE_IDIOM()时,Swift编译器始终崩溃,而没有任何错误消息。很奇怪。
tschoffelen 2015年

首先,Apple的文档现在声明“如果您的应用程序在iOS 3.2及更高版本中运行,请改用userInterfaceIdiom。”
davew

1

如果要检查当前设备是iPad还是iPhone,则可以使用以下代码行:

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

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

  }

1

Swift 3.0

let userInterface = UIDevice.current.userInterfaceIdiom

if(userInterface == .pad){
    //iPads
}else if(userInterface == .phone){
    //iPhone
}else if(userInterface == .carPlay){
    //CarPlay
}else if(userInterface == .tv){
    //AppleTV
}

1

对上述答案做了几处补充,以便您返回类型而不是字符串值。

我认为这主要用于UI调整,因此我认为包括所有子模型(例如iPhone 5s)并不重要,但是可以通过在isDevice Array中添加模型测试来轻松扩展它

经过测试,可以在带有物理和模拟器设备的Swift 3.1 Xcode 8.3.2中工作

实现方式:

UIDevice.whichDevice()

public enum SVNDevice {
  case isiPhone4, isIphone5, isIphone6or7, isIphone6por7p, isIphone, isIpad, isIpadPro
}

extension UIDevice {
  class func whichDevice() -> SVNDevice? {
    let isDevice = { (comparision: Array<(Bool, SVNDevice)>) -> SVNDevice? in
      var device: SVNDevice?
      comparision.forEach({
        device = $0.0 ? $0.1 : device
      })
      return device
    }

    return isDevice([
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0, SVNDevice.isiPhone4),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0, SVNDevice.isIphone5),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0, SVNDevice.isIphone6or7),
      (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0, SVNDevice.isIphone6por7p),
      (UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0, SVNDevice.isIpad),
      (UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1366.0, SVNDevice.isIpadPro)])
  }
}



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

我创建了一个名为SVNBootstaper的框架,其中包括此框架和其他一些辅助协议,该协议是公开的,可以通过迦太基获得。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.