Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iPhone X and other iOS device detection by Swift and Objective-C
struct Device {
// iDevice detection code
static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad
static let IS_IPHONE = UIDevice.current.userInterfaceIdiom == .phone
static let IS_RETINA = UIScreen.main.scale >= 2.0
static let SCREEN_WIDTH = Int(UIScreen.main.bounds.size.width)
static let SCREEN_HEIGHT = Int(UIScreen.main.bounds.size.height)
static let SCREEN_MAX_LENGTH = Int( max(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let SCREEN_MIN_LENGTH = Int( min(SCREEN_WIDTH, SCREEN_HEIGHT) )
static let IS_IPHONE_4 = IS_IPHONE && SCREEN_MAX_LENGTH <= 480 // 2, 3, 3GS, 4, 4S
static let IS_IPHONE_5 = IS_IPHONE && SCREEN_MAX_LENGTH == 568 // 5, 5S, 5C, SE
static let IS_IPHONE_6 = IS_IPHONE && SCREEN_MAX_LENGTH == 667 // 6, 6S, 7, 8
static let IS_IPHONE_6P = IS_IPHONE && SCREEN_MAX_LENGTH == 736 // 6+, 6S+, 7+, 8+
static let IS_IPHONE_XS = IS_IPHONE && SCREEN_MAX_LENGTH == 812 // X, XS, 11 Pro
static let IS_IPHONE_XS_MAX = IS_IPHONE && SCREEN_MAX_LENGTH == 896 // XR, XS Max, 11, 11 Pro Max
static let IS_IPAD_PRO_11 = IS_IPAD && SCREEN_MAX_LENGTH == 1024 // iPad Pro 11"
static let IS_IPAD_PRO_13 = IS_IPAD && SCREEN_MAX_LENGTH == 1366 // iPad Pro 12.9"
}
if (Device.IS_IPHONE_X){
//iPhone X
}
@wongzigii

This comment has been minimized.

Copy link

@wongzigii wongzigii commented Sep 25, 2018

Please update IS_IPHONE_XR/XS/XS Max

@wongzigii

This comment has been minimized.

Copy link

@wongzigii wongzigii commented Sep 25, 2018

static let IS_IPHONE_X_OR_XS	= UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.0
static let IS_IPHONE_XR_OR_XS_MAX = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 896.0
@amiltoncamargo

This comment has been minimized.

Copy link

@amiltoncamargo amiltoncamargo commented Oct 15, 2019

    static let IS_IPHONE_4              = IS_IPHONE && SCREEN_MAX_LENGTH <= 480 // 2, 3, 3GS, 4, 4S
    static let IS_IPHONE_5              = IS_IPHONE && SCREEN_MAX_LENGTH == 568 // 5, 5S, 5C, SE
    static let IS_IPHONE_6              = IS_IPHONE && SCREEN_MAX_LENGTH == 667 // 6, 6S, 7, 8
    static let IS_IPHONE_6P             = IS_IPHONE && SCREEN_MAX_LENGTH == 736 // 6+, 6S+, 7+, 8+
    static let IS_IPHONE_XS             = IS_IPHONE && SCREEN_MAX_LENGTH == 812 // X, XS, 11 Pro
    static let IS_IPHONE_XS_MAX         = IS_IPHONE && SCREEN_MAX_LENGTH == 896 // XR, XS Max, 11, 11 Pro Max
@tiengdung90

This comment has been minimized.

Copy link

@tiengdung90 tiengdung90 commented Dec 11, 2019

Please update

@mahmudahsan

This comment has been minimized.

Copy link
Owner Author

@mahmudahsan mahmudahsan commented Dec 11, 2019

Thank you, I updated.

@zwergius

This comment has been minimized.

Copy link

@zwergius zwergius commented May 12, 2020

static let IS_IPAD_PRO_11 = IS_IPAD && SCREEN_MAX_LENGTH == 1024 // iPad Pro 11"
static let IS_IPAD_PRO_13 = IS_IPAD && SCREEN_MAX_LENGTH == 1366 // iPad Pro 12.9"

@mahmudahsan

This comment has been minimized.

Copy link
Owner Author

@mahmudahsan mahmudahsan commented May 13, 2020

Thank you Zwerge , I updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.