You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.9 KiB

  1. //
  2. // UIDevice+Ext.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon James Kim on 19/08/2019.
  6. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import SystemConfiguration
  9. import UIKit
  10. public extension UIDevice {
  11. static let modelName: String = {
  12. var systemInfo = utsname()
  13. uname(&systemInfo)
  14. let machineMirror = Mirror(reflecting: systemInfo.machine)
  15. let identifier = machineMirror.children.reduce("") { identifier, element in
  16. guard let value = element.value as? Int8, value != 0 else { return identifier }
  17. return identifier + String(UnicodeScalar(UInt8(value)))
  18. }
  19. func mapToDevice(identifier: String) -> String {
  20. #if os(iOS)
  21. switch identifier {
  22. case "iPod5,1":
  23. return "iPod Touch 5"
  24. case "iPod7,1":
  25. return "iPod Touch 6"
  26. case "iPhone3,1", "iPhone3,2", "iPhone3,3":
  27. return "iPhone 4"
  28. case "iPhone4,1":
  29. return "iPhone 4s"
  30. case "iPhone5,1", "iPhone5,2":
  31. return "iPhone 5"
  32. case "iPhone5,3", "iPhone5,4":
  33. return "iPhone 5c"
  34. case "iPhone6,1", "iPhone6,2":
  35. return "iPhone 5s"
  36. case "iPhone7,2":
  37. return "iPhone 6"
  38. case "iPhone7,1":
  39. return "iPhone 6 Plus"
  40. case "iPhone8,1":
  41. return "iPhone 6s"
  42. case "iPhone8,2":
  43. return "iPhone 6s Plus"
  44. case "iPhone9,1", "iPhone9,3":
  45. return "iPhone 7"
  46. case "iPhone9,2", "iPhone9,4":
  47. return "iPhone 7 Plus"
  48. case "iPhone8,4":
  49. return "iPhone SE"
  50. case "iPhone10,1", "iPhone10,4":
  51. return "iPhone 8"
  52. case "iPhone10,2", "iPhone10,5":
  53. return "iPhone 8 Plus"
  54. case "iPhone10,3", "iPhone10,6":
  55. return "iPhone X"
  56. case "iPhone11,2":
  57. return "iPhone XS"
  58. case "iPhone11,4", "iPhone11,6":
  59. return "iPhone XS Max"
  60. case "iPhone11,8":
  61. return "iPhone XR"
  62. case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":
  63. return "iPad 2"
  64. case "iPad3,1", "iPad3,2", "iPad3,3":
  65. return "iPad 3"
  66. case "iPad3,4", "iPad3,5", "iPad3,6":
  67. return "iPad 4"
  68. case "iPad4,1", "iPad4,2", "iPad4,3":
  69. return "iPad Air"
  70. case "iPad5,3", "iPad5,4":
  71. return "iPad Air 2"
  72. case "iPad6,11", "iPad6,12":
  73. return "iPad 5"
  74. case "iPad7,5", "iPad7,6":
  75. return "iPad 6"
  76. case "iPad2,5", "iPad2,6", "iPad2,7":
  77. return "iPad Mini"
  78. case "iPad4,4", "iPad4,5", "iPad4,6":
  79. return "iPad Mini 2"
  80. case "iPad4,7", "iPad4,8", "iPad4,9":
  81. return "iPad Mini 3"
  82. case "iPad5,1", "iPad5,2":
  83. return "iPad Mini 4"
  84. case "iPad6,3", "iPad6,4":
  85. return "iPad Pro (9.7-inch)"
  86. case "iPad6,7", "iPad6,8":
  87. return "iPad Pro (12.9-inch)"
  88. case "iPad7,1", "iPad7,2":
  89. return "iPad Pro (12.9-inch) (2nd generation)"
  90. case "iPad7,3", "iPad7,4":
  91. return "iPad Pro (10.5-inch)"
  92. case "iPad8,1", "iPad8,2", "iPad8,3", "iPad8,4":
  93. return "iPad Pro (11-inch)"
  94. case "iPad8,5", "iPad8,6", "iPad8,7", "iPad8,8":
  95. return "iPad Pro (12.9-inch) (3rd generation)"
  96. case "AppleTV5,3":
  97. return "Apple TV"
  98. case "AppleTV6,2":
  99. return "Apple TV 4K"
  100. case "AudioAccessory1,1":
  101. return "HomePod"
  102. case "i386", "x86_64":
  103. let info = mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "iOS")
  104. return "Simulator \(info)"
  105. default:
  106. return identifier
  107. }
  108. #elseif os(tvOS)
  109. switch identifier {
  110. case "AppleTV5,3":
  111. return "Apple TV 4"
  112. case "AppleTV6,2":
  113. return "Apple TV 4K"
  114. case "i386", "x86_64":
  115. let info = mapToDevice(identifier: ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] ?? "tvOS")
  116. return "Simulator \(info)"
  117. default: return identifier
  118. }
  119. #endif
  120. }
  121. return mapToDevice(identifier: identifier)
  122. }()
  123. }