6

How can I detect running on an iPhone X? I tried the following code.

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)

When I run this macro in the iOS simulator, then IS_IPHONE_X is true. Is this macro correct?

jscs
  • 63,694
  • 13
  • 151
  • 195
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • 2
    The answer to "how do I detect a particular device" has been, for a long time, "don't; ask about the capabilities you need, not the device". You might indeed have a legitimate need for this, but consider carefully. – jscs Sep 13 '17 at 13:20
  • I use: #define IS_IPHONEX (IS_IPHONE_PORTRAIT_SIZE_CLASS([UIScreen mainScreen].traitCollection) && [[UIScreen mainScreen] bounds].size.height == 812.0) – Vlad Sep 19 '17 at 23:33
  • Use UIScreen.mainScreen.fixedCoordinateSpace.bounds.size.height == 812.0 and don't need to worry about the device orientation. – Cheng Zheng Oct 17 '17 at 02:13
  • take a look please https://stackoverflow.com/a/52821290/3472073 – ale_stro Nov 12 '18 at 13:01

3 Answers3

5

According to the Apple Human Interface Guidelines, iPhone X's screen width = 375 and screen height = 812 so, it seems correct I think!

You can write macros something like,

 #define IS_IPHONE4 (([[UIScreen mainScreen] bounds].size.height-480)?NO:YES)

 #define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)

#define IS_IPHONE6 (([[UIScreen mainScreen] bounds].size.height-667)?NO:YES)

#define IS_IPHONE6P (([[UIScreen mainScreen] bounds].size.height-736)?NO:YES)

 #define IS_IPHONEX (([[UIScreen mainScreen] bounds].size.height-812)?NO:YES)
jscs
  • 63,694
  • 13
  • 151
  • 195
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • thats right @Lion iPhone X has [height of 812](https://developer.apple.com/ios/human-interface-guidelines/overview/iphone-x/), which is 20% extra of iPhone 6/7/8 – Pratik Jamariya Sep 13 '17 at 12:12
  • 2
    Will this be a correct macro for landscape mode ? I don't think so – Aditya Srivastava Sep 13 '17 at 12:14
  • no it is for portrait mode only! @AdityaSrivastava – Ketan Parmar Sep 13 '17 at 12:14
  • @AdityaSrivastava you can try something like `#define IS_IPHONE_X (MAX(CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight([[UIScreen mainScreen] bounds])) == 812)`, it'll work for both landscape and portrait i guess – Pratik Jamariya Sep 13 '17 at 12:18
  • yeah we can do like if we want to check for both mode! @PratikJamariya – Ketan Parmar Sep 13 '17 at 12:21
  • #define IS_IPHONEX ((fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )812 ) < DBL_EPSILON) || (fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.width - ( double )812 ) < DBL_EPSILON)) – Levi Oct 24 '17 at 09:35
1

The last line, as far as I know should be a confusion for you. But you are correct

#define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)

Since height for iPhone X in portrait mode is 2436px(812pts) and width in landscape mode is 2436px (812pts).

As Lion suggested for Portrait mode, the below code is for both the modes. You just have to change your lastline of your macro

    #define IS_IPHONE_4 (IS_IPHONE && SCREEN_MAX_LENGTH == 480.0)
//iphone 4
    #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
//iphone 5
    #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
//iphone 6
    #define IS_IPHONE_6p (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
//iphone6p
    #define IS_IPHONE_X (IS_IPHONE && SCREEN_MAX_LENGTH == 812.0)
//iphone x
Aditya Srivastava
  • 2,630
  • 2
  • 13
  • 23
0

Hiii,

Hope this help you.

Objective C

#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_4S (IS_IPHONE && ([[UIScreen mainScreen] bounds].size.height == 480.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
#define IS_IPHONE_5 (IS_IPHONE && ([[UIScreen mainScreen] bounds].size.height == 568.0) && ((IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale) || !IS_OS_8_OR_LATER))
#define   IS_STANDARD_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0  && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale == [UIScreen mainScreen].scale)
#define IS_ZOOMED_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale > [UIScreen mainScreen].scale)
#define IS_STANDARD_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0)
#define IS_ZOOMED_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0 && IS_OS_8_OR_LATER && [UIScreen mainScreen].nativeScale < [UIScreen mainScreen].scale)

you can add more to above list

Swift

let IS_IPAD = (UI_USER_INTERFACE_IDIOM() == .pad)
let IS_IPHONE = (UI_USER_INTERFACE_IDIOM() == .phone)
let IS_IPHONE_4S = (IS_IPHONE && (UIScreen.main.bounds.size.height == 480.0) && ((IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale) || !IS_OS_8_OR_LATER))
let IS_IPHONE_5 = (IS_IPHONE && (UIScreen.main.bounds.size.height == 568.0) && ((IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale) || !IS_OS_8_OR_LATER))
let IS_STANDARD_IPHONE_6 = (IS_IPHONE && UIScreen.main.bounds.size.height == 667.0 && IS_OS_8_OR_LATER && UIScreen.main.nativeScale == UIScreen.main.scale)
let IS_ZOOMED_IPHONE_6 = (IS_IPHONE && UIScreen.main.bounds.size.height == 568.0 && IS_OS_8_OR_LATER && UIScreen.main.nativeScale > UIScreen.main.scale)
let IS_STANDARD_IPHONE_6_PLUS = (IS_IPHONE && UIScreen.main.bounds.size.height == 736.0)

use this macro like :

if (IS_IPAD) {
        // your code hear
}
iamVishal16
  • 1,780
  • 18
  • 40