Guest User

Untitled

a guest
Jan 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. //
  2. // Device.swift
  3. // imHome
  4. //
  5. // Created by Kevin Xu on 2/9/15. Updated on 6/20/15.
  6. // Copyright (c) 2015 Alpha Labs, Inc. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. // MARK: - Device Structure
  12.  
  13. struct Device {
  14.  
  15. // MARK: - Singletons
  16.  
  17. static var TheCurrentDevice: UIDevice {
  18. struct Singleton {
  19. static let device = UIDevice.currentDevice()
  20. }
  21. return Singleton.device
  22. }
  23.  
  24. static var TheCurrentDeviceVersion: Float {
  25. struct Singleton {
  26. static let version = UIDevice.currentDevice().systemVersion.floatValue
  27. }
  28. return Singleton.version
  29. }
  30.  
  31. static var TheCurrentDeviceHeight: CGFloat {
  32. struct Singleton {
  33. static let height = UIScreen.mainScreen().bounds.size.height
  34. }
  35. return Singleton.height
  36. }
  37.  
  38. // MARK: - Device Idiom Checks
  39.  
  40. static var PHONE_OR_PAD: String {
  41. if isPhone() {
  42. return "iPhone"
  43. } else if isPad() {
  44. return "iPad"
  45. }
  46. return "Not iPhone nor iPad"
  47. }
  48.  
  49. static var DEBUG_OR_RELEASE: String {
  50. #if DEBUG
  51. return "Debug"
  52. #else
  53. return "Release"
  54. #endif
  55. }
  56.  
  57. static var SIMULATOR_OR_DEVICE: String {
  58. #if (arch(i386) || arch(x86_64)) && os(iOS)
  59. return "Simulator"
  60. #else
  61. return "Device"
  62. #endif
  63. }
  64.  
  65. static var CURRENT_DEVICE: String {
  66. return GBDeviceInfo.deviceInfo().modelString
  67. }
  68.  
  69. static func isPhone() -> Bool {
  70. return TheCurrentDevice.userInterfaceIdiom == .Phone
  71. }
  72.  
  73. static func isPad() -> Bool {
  74. return TheCurrentDevice.userInterfaceIdiom == .Pad
  75. }
  76.  
  77. static func isDebug() -> Bool {
  78. return DEBUG_OR_RELEASE == "Debug"
  79. }
  80.  
  81. static func isRelease() -> Bool {
  82. return DEBUG_OR_RELEASE == "Release"
  83. }
  84.  
  85. static func isSimulator() -> Bool {
  86. return SIMULATOR_OR_DEVICE == "Simulator"
  87. }
  88.  
  89. static func isDevice() -> Bool {
  90. return SIMULATOR_OR_DEVICE == "Device"
  91. }
  92.  
  93. // MARK: - Device Version Checks
  94.  
  95. enum Versions: Float {
  96. case Five = 5.0
  97. case Six = 6.0
  98. case Seven = 7.0
  99. case Eight = 8.0
  100. case Nine = 9.0
  101. }
  102.  
  103. static func isVersion(version: Versions) -> Bool {
  104. return TheCurrentDeviceVersion >= version.rawValue && TheCurrentDeviceVersion < (version.rawValue + 1.0)
  105. }
  106.  
  107. static func isVersionOrLater(version: Versions) -> Bool {
  108. return TheCurrentDeviceVersion >= version.rawValue
  109. }
  110.  
  111. static func isVersionOrEarlier(version: Versions) -> Bool {
  112. return TheCurrentDeviceVersion < (version.rawValue + 1.0)
  113. }
  114.  
  115. static var CURRENT_VERSION: String {
  116. return "\(TheCurrentDeviceVersion)"
  117. }
  118.  
  119. // MARK: iOS 5 Checks
  120.  
  121. static func IS_OS_5() -> Bool {
  122. return isVersion(.Five)
  123. }
  124.  
  125. static func IS_OS_5_OR_LATER() -> Bool {
  126. return isVersionOrLater(.Five)
  127. }
  128.  
  129. static func IS_OS_5_OR_EARLIER() -> Bool {
  130. return isVersionOrEarlier(.Five)
  131. }
  132.  
  133. // MARK: iOS 6 Checks
  134.  
  135. static func IS_OS_6() -> Bool {
  136. return isVersion(.Six)
  137. }
  138.  
  139. static func IS_OS_6_OR_LATER() -> Bool {
  140. return isVersionOrLater(.Six)
  141. }
  142.  
  143. static func IS_OS_6_OR_EARLIER() -> Bool {
  144. return isVersionOrEarlier(.Six)
  145. }
  146.  
  147. // MARK: iOS 7 Checks
  148.  
  149. static func IS_OS_7() -> Bool {
  150. return isVersion(.Seven)
  151. }
  152.  
  153. static func IS_OS_7_OR_LATER() -> Bool {
  154. return isVersionOrLater(.Seven)
  155. }
  156.  
  157. static func IS_OS_7_OR_EARLIER() -> Bool {
  158. return isVersionOrEarlier(.Seven)
  159. }
  160.  
  161. // MARK: iOS 8 Checks
  162.  
  163. static func IS_OS_8() -> Bool {
  164. return isVersion(.Eight)
  165. }
  166.  
  167. static func IS_OS_8_OR_LATER() -> Bool {
  168. return isVersionOrLater(.Eight)
  169. }
  170.  
  171. static func IS_OS_8_OR_EARLIER() -> Bool {
  172. return isVersionOrEarlier(.Eight)
  173. }
  174.  
  175. // MARK: iOS 9 Checks
  176.  
  177. static func IS_OS_9() -> Bool {
  178. return isVersion(.Nine)
  179. }
  180.  
  181. static func IS_OS_9_OR_LATER() -> Bool {
  182. return isVersionOrLater(.Nine)
  183. }
  184.  
  185. static func IS_OS_9_OR_EARLIER() -> Bool {
  186. return isVersionOrEarlier(.Nine)
  187. }
  188.  
  189. // MARK: - Device Size Checks
  190.  
  191. enum Heights: CGFloat {
  192. case Inches_3_5 = 480
  193. case Inches_4 = 568
  194. case Inches_4_7 = 667
  195. case Inches_5_5 = 736
  196. }
  197.  
  198. static func isSize(height: Heights) -> Bool {
  199. return TheCurrentDeviceHeight == height.rawValue
  200. }
  201.  
  202. static func isSizeOrLarger(height: Heights) -> Bool {
  203. return TheCurrentDeviceHeight >= height.rawValue
  204. }
  205.  
  206. static func isSizeOrSmaller(height: Heights) -> Bool {
  207. return TheCurrentDeviceHeight <= height.rawValue
  208. }
  209.  
  210. static var CURRENT_SIZE: String {
  211. if IS_3_5_INCHES() {
  212. return "3.5 Inches"
  213. } else if IS_4_INCHES() {
  214. return "4 Inches"
  215. } else if IS_4_7_INCHES() {
  216. return "4.7 Inches"
  217. } else if IS_5_5_INCHES() {
  218. return "5.5 Inches"
  219. }
  220. return "\(TheCurrentDeviceHeight) Points"
  221. }
  222.  
  223. // MARK: Retina Check
  224.  
  225. static func IS_RETINA() -> Bool {
  226. return UIScreen.mainScreen().respondsToSelector("scale")
  227. }
  228.  
  229. // MARK: 3.5 Inch Checks
  230.  
  231. static func IS_3_5_INCHES() -> Bool {
  232. return isPhone() && isSize(.Inches_3_5)
  233. }
  234.  
  235. static func IS_3_5_INCHES_OR_LARGER() -> Bool {
  236. return isPhone() && isSizeOrLarger(.Inches_3_5)
  237. }
  238.  
  239. static func IS_3_5_INCHES_OR_SMALLER() -> Bool {
  240. return isPhone() && isSizeOrSmaller(.Inches_3_5)
  241. }
  242.  
  243. // MARK: 4 Inch Checks
  244.  
  245. static func IS_4_INCHES() -> Bool {
  246. return isPhone() && isSize(.Inches_4)
  247. }
  248.  
  249. static func IS_4_INCHES_OR_LARGER() -> Bool {
  250. return isPhone() && isSizeOrLarger(.Inches_4)
  251. }
  252.  
  253. static func IS_4_INCHES_OR_SMALLER() -> Bool {
  254. return isPhone() && isSizeOrSmaller(.Inches_4)
  255. }
  256.  
  257. // MARK: 4.7 Inch Checks
  258.  
  259. static func IS_4_7_INCHES() -> Bool {
  260. return isPhone() && isSize(.Inches_4_7)
  261. }
  262.  
  263. static func IS_4_7_INCHES_OR_LARGER() -> Bool {
  264. return isPhone() && isSizeOrLarger(.Inches_4_7)
  265. }
  266.  
  267. static func IS_4_7_INCHES_OR_SMALLER() -> Bool {
  268. return isPhone() && isSizeOrLarger(.Inches_4_7)
  269. }
  270.  
  271. // MARK: 5.5 Inch Checks
  272.  
  273. static func IS_5_5_INCHES() -> Bool {
  274. return isPhone() && isSize(.Inches_5_5)
  275. }
  276.  
  277. static func IS_5_5_INCHES_OR_LARGER() -> Bool {
  278. return isPhone() && isSizeOrLarger(.Inches_5_5)
  279. }
  280.  
  281. static func IS_5_5_INCHES_OR_SMALLER() -> Bool {
  282. return isPhone() && isSizeOrLarger(.Inches_5_5)
  283. }
  284.  
  285. // MARK: - International Checks
  286.  
  287. static var CURRENT_REGION: String {
  288. return NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as! String
  289. }
  290. }
Add Comment
Please, Sign In to add comment