Skip to content

Instantly share code, notes, and snippets.

@imkevinxu
Last active March 4, 2023 16:09
Show Gist options
  • Select an option

  • Save imkevinxu/2bb1197552b095ab25c8 to your computer and use it in GitHub Desktop.

Select an option

Save imkevinxu/2bb1197552b095ab25c8 to your computer and use it in GitHub Desktop.
iOS device checks for OS version and screen size in Swift
//
// Device.swift
// imHome
//
// Created by Kevin Xu on 2/9/15.
// Copyright (c) 2015 Alpha Labs, Inc. All rights reserved.
//
import Foundation
// MARK: - Device Class
class Device {
// MARK: - Device Version Checks
// MARK: iOS 5 Checks
class func IS_OS_5() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 5.0
&& UIDevice.currentDevice().systemVersion.floatValue < 6.0
}
class func IS_OS_5_OR_LATER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 5.0
}
class func IS_OS_5_OR_EARLIER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue < 6.0
}
// MARK: iOS 6 Checks
class func IS_OS_6() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 6.0
&& UIDevice.currentDevice().systemVersion.floatValue < 7.0
}
class func IS_OS_6_OR_LATER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 6.0
}
class func IS_OS_6_OR_EARLIER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue < 7.0
}
// MARK: iOS 7 Checks
class func IS_OS_7() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 7.0
&& UIDevice.currentDevice().systemVersion.floatValue < 8.0
}
class func IS_OS_7_OR_LATER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 7.0
}
class func IS_OS_7_OR_EARLIER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue < 8.0
}
// MARK: iOS 8 Checks
class func IS_OS_8() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 8.0
&& UIDevice.currentDevice().systemVersion.floatValue < 9.0
}
class func IS_OS_8_OR_LATER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue >= 8.0
}
class func IS_OS_8_OR_EARLIER() -> Bool {
return UIDevice.currentDevice().systemVersion.floatValue < 9.0
}
// MARK: - Device Size Checks
// MARK: Retina Check
class func IS_RETINA() -> Bool {
return UIScreen.mainScreen().respondsToSelector("scale")
}
// MARK: 3.5 Inch Checks
class func IS_3_5_INCHES() -> Bool {
return UIScreen.mainScreen().bounds.size.height == 480
}
class func IS_3_5_INCHES_OR_LARGER() -> Bool {
return UIScreen.mainScreen().bounds.size.height >= 480
}
class func IS_3_5_INCHES_OR_SMALLER() -> Bool {
return UIScreen.mainScreen().bounds.size.height <= 480
}
// MARK: 4 Inch Checks
class func IS_4_INCHES() -> Bool {
return UIScreen.mainScreen().bounds.size.height == 568
}
class func IS_4_INCHES_OR_LARGER() -> Bool {
return UIScreen.mainScreen().bounds.size.height >= 568
}
class func IS_4_INCHES_OR_SMALLER() -> Bool {
return UIScreen.mainScreen().bounds.size.height <= 568
}
// MARK: 4.7 Inch Checks
class func IS_4_7_INCHES() -> Bool {
return UIScreen.mainScreen().bounds.size.height == 667
}
class func IS_4_7_INCHES_OR_LARGER() -> Bool {
return UIScreen.mainScreen().bounds.size.height >= 667
}
class func IS_4_7_INCHES_OR_SMALLER() -> Bool {
return UIScreen.mainScreen().bounds.size.height <= 667
}
// MARK: 5.5 Inch Checks
class func IS_5_5_INCHES() -> Bool {
return UIScreen.mainScreen().bounds.size.height == 736
}
class func IS_5_5_INCHES_OR_LARGER() -> Bool {
return UIScreen.mainScreen().bounds.size.height >= 736
}
class func IS_5_5_INCHES_OR_SMALLER() -> Bool {
return UIScreen.mainScreen().bounds.size.height <= 736
}
}
@AndreiBoariu
Copy link

Nice class, and very useful.. Thanks man

@vesakk
Copy link

vesakk commented Sep 1, 2015

Very nice! Thank you!

@KimBin
Copy link

KimBin commented Oct 20, 2015

what's meaning of "GBDeviceInfo"

@oddmagne
Copy link

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'"

@vitorspencer
Copy link

@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 :)

@norsez
Copy link

norsez commented Jan 12, 2016

Very handy. Love this. Thanks.

@brandonscript
Copy link

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

@dipottor
Copy link

Very useful utility .. Thank you

@sourleangchhean168
Copy link

Great. very useful...

@smartkuk
Copy link

i love it!

@RodrigoLGuimaraes
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment