[Swift] Get devices model range

Here a simple and easy memo for getting the iPhones devices models range.

I’ve used the nativeBounds property, available from iOS 8+:

var nativeBounds: CGRect { get }

This rectangle is based on the device in a portrait-up orientation. This value does not change as the device rotates.

Apple: https://developer.apple.com/documentation/uikit/uiscreen/1617810-nativebounds 

Code:
// Device range

enum DeviceType {
    case iPhone_4
    case iPhone_5_SE
    case iPhone_6s_7_8
    case iPhone_6sp_7p_8p
    case iPhone_X
    case Unknown
}

Now you can get your device range. For any use you want.

// get device range by native size

static func deviceRange() -> DeviceType {
    if UIScreen.main.nativeBounds.height == 480 {
        return .iPhone_4
    }

    if UIScreen.main.nativeBounds.height == 1136 {
        return .iPhone_5_SE
    }

    if UIScreen.main.nativeBounds.height == 1334 {
        return .iPhone_6s_7_8
    }

    if UIScreen.main.nativeBounds.height == 2208 {
        return .iPhone_6sp_7p_8p
    }

    if UIScreen.main.nativeBounds.height == 2436 {
        return .iPhone_X
    }

     return .Unknown
}
Example:
if DeviceUtils.deviceRange() == .iPhone_4 {
    exit(0) // lol
}

 

done.

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.