Advertisement
luck-alex13

DeviceType Class

Oct 18th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.18 KB | None | 0 0
  1. public enum DeviceType: String, CaseIterable {
  2.     case iPhone2G
  3.    
  4.     case iPhone3G
  5.     case iPhone3GS
  6.    
  7.     case iPhone4
  8.     case iPhone4S
  9.    
  10.     case iPhone5
  11.     case iPhone5C
  12.     case iPhone5S
  13.    
  14.     case iPhone6
  15.     case iPhone6Plus
  16.    
  17.     case iPhone6S
  18.     case iPhone6SPlus
  19.    
  20.     case iPhoneSE
  21.    
  22.     case iPhone7
  23.     case iPhone7Plus
  24.    
  25.     case iPhone8
  26.     case iPhone8Plus
  27.    
  28.     case iPhoneX
  29.    
  30.     case iPodTouch1G
  31.     case iPodTouch2G
  32.     case iPodTouch3G
  33.     case iPodTouch4G
  34.     case iPodTouch5G
  35.     case iPodTouch6G
  36.    
  37.     case iPad
  38.     case iPad2
  39.     case iPad3
  40.     case iPad4
  41.     case iPadMini
  42.     case iPadMiniRetina
  43.     case iPadMini3
  44.     case iPadMini4
  45.    
  46.     case iPadAir
  47.     case iPadAir2
  48.    
  49.     case iPadPro9Inch
  50.     case iPadPro10p5Inch
  51.     case iPadPro12Inch
  52.    
  53.     case simulator
  54.     case notAvailable
  55.    
  56.     // MARK: Constants
  57.     /// The current device type
  58.     public static var current: DeviceType {
  59.        
  60.         var systemInfo = utsname()
  61.         uname(&systemInfo)
  62.        
  63.         let machine = systemInfo.machine
  64.         let mirror = Mirror(reflecting: machine)
  65.         var identifier = ""
  66.        
  67.         for child in mirror.children {
  68.             if let value = child.value as? Int8, value != 0 {
  69.                 identifier.append(String(UnicodeScalar(UInt8(value))))
  70.             }
  71.         }
  72.        
  73.         return DeviceType(identifier: identifier)
  74.     }
  75.    
  76.     // MARK: Variables
  77.     /// The display name of the device type
  78.     public var displayName: String {
  79.        
  80.         switch self {
  81.         case .iPhone2G: return "iPhone 2G"
  82.         case .iPhone3G: return "iPhone 3G"
  83.         case .iPhone3GS: return "iPhone 3GS"
  84.         case .iPhone4: return "iPhone 4"
  85.         case .iPhone4S: return "iPhone 4S"
  86.         case .iPhone5: return "iPhone 5"
  87.         case .iPhone5C: return "iPhone 5C"
  88.         case .iPhone5S: return "iPhone 5S"
  89.         case .iPhone6Plus: return "iPhone 6 Plus"
  90.         case .iPhone6: return "iPhone 6"
  91.         case .iPhone6S: return "iPhone 6S"
  92.         case .iPhone6SPlus: return "iPhone 6S Plus"
  93.         case .iPhoneSE: return "iPhone SE"
  94.         case .iPhone7: return "iPhone 7"
  95.         case .iPhone7Plus: return "iPhone 7 Plus"
  96.         case .iPhone8: return "iPhone 8"
  97.         case .iPhone8Plus: return "iPhone 8 Plus"
  98.         case .iPhoneX: return "iPhone X"
  99.         case .iPodTouch1G: return "iPod Touch 1G"
  100.         case .iPodTouch2G: return "iPod Touch 2G"
  101.         case .iPodTouch3G: return "iPod Touch 3G"
  102.         case .iPodTouch4G: return "iPod Touch 4G"
  103.         case .iPodTouch5G: return "iPod Touch 5G"
  104.         case .iPodTouch6G: return "iPod Touch 6G"
  105.         case .iPad: return "iPad"
  106.         case .iPad2: return "iPad 2"
  107.         case .iPad3: return "iPad 3"
  108.         case .iPad4: return "iPad 4"
  109.         case .iPadMini: return "iPad Mini"
  110.         case .iPadMiniRetina: return "iPad Mini Retina"
  111.         case .iPadMini3: return "iPad Mini 3"
  112.         case .iPadMini4: return "iPad Mini 4"
  113.         case .iPadAir: return "iPad Air"
  114.         case .iPadAir2: return "iPad Air 2"
  115.         case .iPadPro9Inch: return "iPad Pro 9 Inch"
  116.         case .iPadPro10p5Inch: return "iPad Pro 10.5 Inch"
  117.         case .iPadPro12Inch: return "iPad Pro 12 Inch"
  118.         case .simulator: return "Simulator"
  119.         case .notAvailable: return "Not Available"
  120.         }
  121.     }
  122.    
  123.     /// The identifiers associated with each device type
  124.     internal var identifiers: [String] {
  125.        
  126.         switch self {
  127.         case .notAvailable: return []
  128.         case .simulator: return ["i386", "x86_64"]
  129.            
  130.         case .iPhone2G: return ["iPhone1,1"]
  131.         case .iPhone3G: return ["iPhone1,2"]
  132.         case .iPhone3GS: return ["iPhone2,1"]
  133.         case .iPhone4: return ["iPhone3,1", "iPhone3,2", "iPhone3,3"]
  134.         case .iPhone4S: return ["iPhone4,1"]
  135.         case .iPhone5: return ["iPhone5,1", "iPhone5,2"]
  136.         case .iPhone5C: return ["iPhone5,3", "iPhone5,4"]
  137.         case .iPhone5S: return ["iPhone6,1", "iPhone6,2"]
  138.         case .iPhone6: return ["iPhone7,2"]
  139.         case .iPhone6Plus: return ["iPhone7,1"]
  140.         case .iPhone6S: return ["iPhone8,1"]
  141.         case .iPhone6SPlus: return ["iPhone8,2"]
  142.         case .iPhoneSE: return ["iPhone8,4"]
  143.         case .iPhone7: return ["iPhone9,1", "iPhone9,3"]
  144.         case .iPhone7Plus: return ["iPhone9,2", "iPhone9,4"]
  145.         case .iPhone8: return ["iPhone10,1", "iPhone10,4"]
  146.         case .iPhone8Plus: return ["iPhone10,2", "iPhone10,5"]
  147.         case .iPhoneX: return ["iPhone10,3", "iPhone10,6"]
  148.            
  149.         case .iPodTouch1G: return ["iPod1,1"]
  150.         case .iPodTouch2G: return ["iPod2,1"]
  151.         case .iPodTouch3G: return ["iPod3,1"]
  152.         case .iPodTouch4G: return ["iPod4,1"]
  153.         case .iPodTouch5G: return ["iPod5,1"]
  154.         case .iPodTouch6G: return ["iPod7,1"]
  155.            
  156.         case .iPad: return ["iPad1,1", "iPad1,2"]
  157.         case .iPad2: return ["iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4"]
  158.         case .iPad3: return ["iPad3,1", "iPad3,2", "iPad3,3"]
  159.         case .iPad4: return ["iPad3,4", "iPad3,5", "iPad3,6"]
  160.         case .iPadMini: return ["iPad2,5", "iPad2,6", "iPad2,7"]
  161.         case .iPadMiniRetina: return ["iPad4,4", "iPad4,5", "iPad4,6"]
  162.         case .iPadMini3: return ["iPad4,7", "iPad4,8", "iPad4,9"]
  163.         case .iPadMini4: return ["iPad5,1", "iPad5,2"]
  164.         case .iPadAir: return ["iPad4,1", "iPad4,2", "iPad4,3"]
  165.         case .iPadAir2: return ["iPad5,3", "iPad5,4"]
  166.         case .iPadPro9Inch: return ["iPad6,3", "iPad6,4"]
  167.         case .iPadPro10p5Inch: return ["iPad7,3", "iPad7,4"]
  168.         case .iPadPro12Inch: return ["iPad6,7", "iPad6,8", "iPad7,1", "iPad7,2"]
  169.         }
  170.     }
  171.    
  172.     // MARK: Inits
  173.     /// Creates a device type
  174.     ///
  175.     /// - Parameter identifier: The identifier of the device
  176.     internal init(identifier: String) {
  177.        
  178.         for device in DeviceType.allCases {
  179.             for deviceId in device.identifiers {
  180.                 guard identifier == deviceId else { continue }
  181.                 self = device
  182.                 return
  183.             }
  184.         }
  185.        
  186.         self = .notAvailable
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement