-
-
Save imkevinxu/2bb1197552b095ab25c8 to your computer and use it in GitHub Desktop.
| // | |
| // Device.swift | |
| // imHome | |
| // | |
| // Created by Kevin Xu on 2/9/15. | |
| // Copyright (c) 2015 Alpha Labs, Inc. All rights reserved. | |
| // | |
| import Foundation | |
| // MARK: - Device Structure | |
| struct Device { | |
| // MARK: - Singletons | |
| static var currentDeviceVersion: Float { | |
| struct Singleton { | |
| static let version = UIDevice.currentDevice().systemVersion.floatValue | |
| } | |
| return Singleton.version | |
| } | |
| static var currentDeviceHeight: CGFloat { | |
| struct Singleton { | |
| static let height = UIScreen.mainScreen().bounds.size.height | |
| } | |
| return Singleton.height | |
| } | |
| // MARK: - Device Version Checks | |
| // MARK: iOS 5 Checks | |
| static func IS_OS_5() -> Bool { | |
| return currentDeviceVersion >= 5.0 && currentDeviceVersion < 6.0 | |
| } | |
| static func IS_OS_5_OR_LATER() -> Bool { | |
| return currentDeviceVersion >= 5.0 | |
| } | |
| static func IS_OS_5_OR_EARLIER() -> Bool { | |
| return currentDeviceVersion < 6.0 | |
| } | |
| // MARK: iOS 6 Checks | |
| static func IS_OS_6() -> Bool { | |
| return currentDeviceVersion >= 6.0 && currentDeviceVersion < 7.0 | |
| } | |
| static func IS_OS_6_OR_LATER() -> Bool { | |
| return currentDeviceVersion >= 6.0 | |
| } | |
| static func IS_OS_6_OR_EARLIER() -> Bool { | |
| return currentDeviceVersion < 7.0 | |
| } | |
| // MARK: iOS 7 Checks | |
| static func IS_OS_7() -> Bool { | |
| return currentDeviceVersion >= 7.0 && currentDeviceVersion < 8.0 | |
| } | |
| static func IS_OS_7_OR_LATER() -> Bool { | |
| return currentDeviceVersion >= 7.0 | |
| } | |
| static func IS_OS_7_OR_EARLIER() -> Bool { | |
| return currentDeviceVersion < 8.0 | |
| } | |
| // MARK: iOS 8 Checks | |
| static func IS_OS_8() -> Bool { | |
| return currentDeviceVersion >= 8.0 && currentDeviceVersion < 9.0 | |
| } | |
| static func IS_OS_8_OR_LATER() -> Bool { | |
| return currentDeviceVersion >= 8.0 | |
| } | |
| static func IS_OS_8_OR_EARLIER() -> Bool { | |
| return currentDeviceVersion < 9.0 | |
| } | |
| // MARK: - Device Size Checks | |
| // MARK: Retina Check | |
| static func IS_RETINA() -> Bool { | |
| return UIScreen.mainScreen().respondsToSelector("scale") | |
| } | |
| // MARK: 3.5 Inch Checks | |
| static func IS_3_5_INCHES() -> Bool { | |
| return currentDeviceHeight == 480 | |
| } | |
| static func IS_3_5_INCHES_OR_LARGER() -> Bool { | |
| return currentDeviceHeight >= 480 | |
| } | |
| static func IS_3_5_INCHES_OR_SMALLER() -> Bool { | |
| return currentDeviceHeight <= 480 | |
| } | |
| // MARK: 4 Inch Checks | |
| static func IS_4_INCHES() -> Bool { | |
| return currentDeviceHeight == 568 | |
| } | |
| static func IS_4_INCHES_OR_LARGER() -> Bool { | |
| return currentDeviceHeight >= 568 | |
| } | |
| static func IS_4_INCHES_OR_SMALLER() -> Bool { | |
| return currentDeviceHeight <= 568 | |
| } | |
| // MARK: 4.7 Inch Checks | |
| static func IS_4_7_INCHES() -> Bool { | |
| return currentDeviceHeight == 667 | |
| } | |
| static func IS_4_7_INCHES_OR_LARGER() -> Bool { | |
| return currentDeviceHeight >= 667 | |
| } | |
| static func IS_4_7_INCHES_OR_SMALLER() -> Bool { | |
| return currentDeviceHeight <= 667 | |
| } | |
| // MARK: 5.5 Inch Checks | |
| static func IS_5_5_INCHES() -> Bool { | |
| return currentDeviceHeight == 736 | |
| } | |
| static func IS_5_5_INCHES_OR_LARGER() -> Bool { | |
| return currentDeviceHeight >= 736 | |
| } | |
| static func IS_5_5_INCHES_OR_SMALLER() -> Bool { | |
| return currentDeviceHeight <= 736 | |
| } | |
| } |
Very nice! Thank you!
what's meaning of "GBDeviceInfo"
I get two errors during compile.
static let version = UIDevice.currentDevice().systemVersion.floatValue
error: "Value of type 'String' has no member 'floatValue'"
return GBDeviceInfo.deviceInfo().modelString
error: "Use of unresolved identifier 'GBDeviceInfo'"
@KimBin and @oddmagne:
replace
static let version = UIDevice.currentDevice().systemVersion.floatValue
with
static let version = (UIDevice.currentDevice().systemVersion as NSString).floatValue
Also add https://github.com/lmirosevic/GBDeviceInfo to your project.
@imkevinxu: very cool utility! Thanks a lot :)
Very handy. Love this. Thanks.
Works great, except from what I can see deviceHeight only works if the device is in portrait; otherwise in landscape, you'll get the height of the display (which is the width of the device).
static var TheCurrentDeviceHeight: CGFloat {
struct Singleton {
static let height = max(UIScreen.mainScreen().bounds.size.height, UIScreen.mainScreen().bounds.size.width)
}
return Singleton.height
}
... should do it
Very useful utility .. Thank you
Great. very useful...
i love it!
Ported it to Swift4: https://gist.github.com/RodrigoLGuimaraes/90086a28107809f58a0133e3e11419d3
Nice class, and very useful.. Thanks man