Created
June 19, 2018 12:01
-
-
Save soroyv/53aead1656d8d8fe0c49e2daf86d0567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum iOSDevice { | |
| case isIphoneSE, isIphone5, isIphone6, isIphone6plus, isIphone7, isIphone7plus, isIphone8, isIphone8plus, isIphoneX, isIpad, isIpadPro | |
| } | |
| extension UIDevice { | |
| public class func whichDevice() -> iOSDevice? { | |
| let isDevice = { (comparision: Array<(Bool, iOSDevice)>) -> iOSDevice? in | |
| var device: iOSDevice? | |
| comparision.forEach({ | |
| device = $0.0 ? $0.1 : device | |
| }) | |
| return device | |
| } | |
| return isDevice([ | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0, iOSDevice.isIphoneSE), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0, iOSDevice.isIphone5), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0, iOSDevice.isIphone6), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0, iOSDevice.isIphone6plus), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0, iOSDevice.isIphone7), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0, iOSDevice.isIphone7plus), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0, iOSDevice.isIphone8), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0, iOSDevice.isIphone8plus), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.0, iOSDevice.isIphoneX), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH >= 568.0, iOSDevice.isIpad), | |
| (UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 1366.0, iOSDevice.isIpadPro)]) | |
| } | |
| public class func whichVersion() -> iOSVersion? { | |
| let systemOS = (UIDevice.current.systemVersion as NSString).floatValue | |
| let isVersion = { (comparisons: Array<(Bool, iOSVersion)>) ->iOSVersion? in | |
| var version: iOSVersion? | |
| comparisons.forEach({ | |
| version = $0.0 ? $0.1 : version | |
| }) | |
| return version | |
| } | |
| return isVersion([(systemOS < 8.0 && systemOS >= 7.0, iOSVersion.iOS7), | |
| (systemOS >= 8.0 && systemOS < 9.0, iOSVersion.iOS8), | |
| (systemOS >= 9.0 && systemOS < 10.0, iOSVersion.iOS9), | |
| (systemOS >= 10.0 && systemOS < 11.0, iOSVersion.iOS10), | |
| (systemOS >= 11.0 && systemOS < 12.0, iOSVersion.iOS11)]) | |
| } | |
| public class func isSmallIphone() -> Bool { | |
| let device = whichDevice() | |
| if device == .isIphoneSE || device == .isIphone5 { | |
| return true | |
| } | |
| return false | |
| } | |
| } | |
| private struct ScreenSize { | |
| static let SCREEN_WIDTH = UIScreen.main.bounds.size.width | |
| static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height | |
| static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT) | |
| static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT) | |
| } | |
| public enum iOSVersion { | |
| case iOS7, iOS8, iOS9, iOS10, iOS11 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment