UIDeviceExtensions.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // UIDeviceExtensions.swift
  3. // EZSwiftExtensions
  4. //
  5. // Created by Goktug Yilmaz on 15/07/15.
  6. // Copyright (c) 2015 Goktug Yilmaz. All rights reserved.
  7. //
  8. #if os(iOS) || os(tvOS)
  9. import UIKit
  10. /// EZSwiftExtensions
  11. private let DeviceList = [
  12. /* iPod 5 */ "iPod5,1": "iPod Touch 5",
  13. /* iPod 6 */ "iPod7,1": "iPod Touch 6",
  14. /* iPhone 4 */ "iPhone3,1": "iPhone 4", "iPhone3,2": "iPhone 4", "iPhone3,3": "iPhone 4",
  15. /* iPhone 4S */ "iPhone4,1": "iPhone 4S",
  16. /* iPhone 5 */ "iPhone5,1": "iPhone 5", "iPhone5,2": "iPhone 5",
  17. /* iPhone 5C */ "iPhone5,3": "iPhone 5C", "iPhone5,4": "iPhone 5C",
  18. /* iPhone 5S */ "iPhone6,1": "iPhone 5S", "iPhone6,2": "iPhone 5S",
  19. /* iPhone 6 */ "iPhone7,2": "iPhone 6",
  20. /* iPhone 6 Plus */ "iPhone7,1": "iPhone 6 Plus",
  21. /* iPhone 6S */ "iPhone8,1": "iPhone 6S",
  22. /* iPhone 6S Plus */ "iPhone8,2": "iPhone 6S Plus",
  23. /* iPhone 7 */ "iPhone9,1": "iPhone 7", "iPhone9,3": "iPhone 7",
  24. /* iPhone 7 Plus */ "iPhone9,2": "iPhone 7 Plus", "iPhone9,4": "iPhone 7 Plus",
  25. /* iPhone SE */ "iPhone8,4": "iPhone SE",
  26. /* iPhone 8 */ "iPhone10,1": "iPhone 8", "iPhone10,4": "iPhone 8",
  27. /* iPhone 8 Plus */ "iPhone10,2": "iPhone 8 Plus", "iPhone10,5": "iPhone 8 Plus",
  28. /* iPhone X */ "iPhone10,3": "iPhone X", "iPhone10,6": "iPhone X",
  29. /* iPhone XS */ "iPhone11,2": "iPhone XS",
  30. /* iPhone XS Max */ "iPhone11,4": "iPhone XS Max", "iPhone11,6": "iPhone XS Max",
  31. /* iPhone XR */ "iPhone11,8": "iPhone XR",
  32. /* iPhone 11 */ "iPhone12,1": "iPhone 11",
  33. /* iPhone 11 Pro*/ "iPhone12,3": "iPhone 11 Pro",
  34. /* iPhone 11 Pro Max*/"iPhone12,5": "iPhone 11 Pro Max",
  35. /* iPhone SE 2*/ "iPhone12,8": "iPhone SE 2",
  36. /* iPad 2 */ "iPad2,1": "iPad 2", "iPad2,2": "iPad 2", "iPad2,3": "iPad 2", "iPad2,4": "iPad 2",
  37. /* iPad 3 */ "iPad3,1": "iPad 3", "iPad3,2": "iPad 3", "iPad3,3": "iPad 3",
  38. /* iPad 4 */ "iPad3,4": "iPad 4", "iPad3,5": "iPad 4", "iPad3,6": "iPad 4",
  39. /* iPad Air */ "iPad4,1": "iPad Air", "iPad4,2": "iPad Air", "iPad4,3": "iPad Air",
  40. /* iPad Air 2 */ "iPad5,3": "iPad Air 2", "iPad5,4": "iPad Air 2",
  41. /* iPad Mini */ "iPad2,5": "iPad Mini", "iPad2,6": "iPad Mini", "iPad2,7": "iPad Mini",
  42. /* iPad Mini 2 */ "iPad4,4": "iPad Mini 2", "iPad4,5": "iPad Mini 2", "iPad4,6": "iPad Mini 2",
  43. /* iPad Mini 3 */ "iPad4,7": "iPad Mini 3", "iPad4,8": "iPad Mini 3", "iPad4,9": "iPad Mini 3",
  44. /* iPad Mini 4 */ "iPad5,1": "iPad Mini 4", "iPad5,2": "iPad Mini 4",
  45. /* iPad Pro */ "iPad6,7": "iPad Pro", "iPad6,8": "iPad Pro",
  46. /* AppleTV */ "AppleTV5,3": "AppleTV",
  47. /* Simulator */ "x86_64": "Simulator", "i386": "Simulator"
  48. ]
  49. extension UIDevice {
  50. /// EZSwiftExtensions
  51. public class func idForVendor() -> String? {
  52. return UIDevice.current.identifierForVendor?.uuidString
  53. }
  54. /// EZSwiftExtensions - Operating system name
  55. public class func systemName() -> String {
  56. return UIDevice.current.systemName
  57. }
  58. /// EZSwiftExtensions - Operating system version
  59. public class func systemVersion() -> String {
  60. return UIDevice.current.systemVersion
  61. }
  62. /// EZSwiftExtensions - Operating system version
  63. public class func systemFloatVersion() -> Float {
  64. return (systemVersion() as NSString).floatValue
  65. }
  66. /// EZSwiftExtensions
  67. public class func deviceName() -> String {
  68. return UIDevice.current.name
  69. }
  70. /// EZSwiftExtensions
  71. public class func deviceLanguage() -> String {
  72. return Bundle.main.preferredLocalizations[0]
  73. }
  74. /// EZSwiftExtensions
  75. public class func deviceModelReadable() -> String {
  76. return DeviceList[deviceModel()] ?? deviceModel()
  77. }
  78. /// EZSE: Returns true if the device is iPhone //TODO: Add to readme
  79. public class func isPhone() -> Bool {
  80. return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone
  81. }
  82. /// EZSE: Returns true if the device is iPad //TODO: Add to readme
  83. public class func isPad() -> Bool {
  84. return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
  85. }
  86. /// EZSwiftExtensions
  87. public class func deviceModel() -> String {
  88. var systemInfo = utsname()
  89. uname(&systemInfo)
  90. let machine = systemInfo.machine
  91. var identifier = ""
  92. let mirror = Mirror(reflecting: machine)
  93. for child in mirror.children {
  94. let value = child.value
  95. if let value = value as? Int8, value != 0 {
  96. identifier.append(String(UnicodeScalar(UInt8(value))))
  97. }
  98. }
  99. return identifier
  100. }
  101. //TODO: Fix syntax, add docs and readme for these methods:
  102. //TODO: Delete isSystemVersionOver()
  103. // MARK: - Device Version Checks
  104. public enum Versions: Float {
  105. case five = 5.0
  106. case six = 6.0
  107. case seven = 7.0
  108. case eight = 8.0
  109. case nine = 9.0
  110. case ten = 10.0
  111. }
  112. public class func isVersion(_ version: Versions) -> Bool {
  113. return systemFloatVersion() >= version.rawValue && systemFloatVersion() < (version.rawValue + 1.0)
  114. }
  115. public class func isVersionOrLater(_ version: Versions) -> Bool {
  116. return systemFloatVersion() >= version.rawValue
  117. }
  118. public class func isVersionOrEarlier(_ version: Versions) -> Bool {
  119. return systemFloatVersion() < (version.rawValue + 1.0)
  120. }
  121. public class var CURRENT_VERSION: String {
  122. return "\(systemFloatVersion())"
  123. }
  124. // MARK: iOS 5 Checks
  125. public class func IS_OS_5() -> Bool {
  126. return isVersion(.five)
  127. }
  128. public class func IS_OS_5_OR_LATER() -> Bool {
  129. return isVersionOrLater(.five)
  130. }
  131. public class func IS_OS_5_OR_EARLIER() -> Bool {
  132. return isVersionOrEarlier(.five)
  133. }
  134. // MARK: iOS 6 Checks
  135. public class func IS_OS_6() -> Bool {
  136. return isVersion(.six)
  137. }
  138. public class func IS_OS_6_OR_LATER() -> Bool {
  139. return isVersionOrLater(.six)
  140. }
  141. public class func IS_OS_6_OR_EARLIER() -> Bool {
  142. return isVersionOrEarlier(.six)
  143. }
  144. // MARK: iOS 7 Checks
  145. public class func IS_OS_7() -> Bool {
  146. return isVersion(.seven)
  147. }
  148. public class func IS_OS_7_OR_LATER() -> Bool {
  149. return isVersionOrLater(.seven)
  150. }
  151. public class func IS_OS_7_OR_EARLIER() -> Bool {
  152. return isVersionOrEarlier(.seven)
  153. }
  154. // MARK: iOS 8 Checks
  155. public class func IS_OS_8() -> Bool {
  156. return isVersion(.eight)
  157. }
  158. public class func IS_OS_8_OR_LATER() -> Bool {
  159. return isVersionOrLater(.eight)
  160. }
  161. public class func IS_OS_8_OR_EARLIER() -> Bool {
  162. return isVersionOrEarlier(.eight)
  163. }
  164. // MARK: iOS 9 Checks
  165. public class func IS_OS_9() -> Bool {
  166. return isVersion(.nine)
  167. }
  168. public class func IS_OS_9_OR_LATER() -> Bool {
  169. return isVersionOrLater(.nine)
  170. }
  171. public class func IS_OS_9_OR_EARLIER() -> Bool {
  172. return isVersionOrEarlier(.nine)
  173. }
  174. // MARK: iOS 10 Checks
  175. public class func IS_OS_10() -> Bool {
  176. return isVersion(.ten)
  177. }
  178. public class func IS_OS_10_OR_LATER() -> Bool {
  179. return isVersionOrLater(.ten)
  180. }
  181. public class func IS_OS_10_OR_EARLIER() -> Bool {
  182. return isVersionOrEarlier(.ten)
  183. }
  184. /// EZSwiftExtensions
  185. public class func isSystemVersionOver(_ requiredVersion: String) -> Bool {
  186. switch systemVersion().compare(requiredVersion, options: NSString.CompareOptions.numeric) {
  187. case .orderedSame, .orderedDescending:
  188. //println("iOS >= 8.0")
  189. return true
  190. case .orderedAscending:
  191. //println("iOS < 8.0")
  192. return false
  193. }
  194. }
  195. }
  196. #endif