UIDeviceExtensions.swift 7.4 KB

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