Detect If the Device Is Iphone X

Detect if the device is iPhone X

Based on your question, the answer is no. There are no direct methods. For more information you can get the information here:

  • How to get device make and model on iOS?

and

  • how to check screen size of iphone 4 and iphone 5 programmatically in swift

The iPhone X height is 2436 px

From Device Screen Sizes and resolutions:

Sample Image

From Device Screen Sizes and Orientations:

Sample Image

Swift 3 and later:

if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")

case 1334:
print("iPhone 6/6S/7/8")

case 1920, 2208:
print("iPhone 6+/6S+/7+/8+")

case 2436:
print("iPhone X/XS/11 Pro")

case 2688:
print("iPhone XS Max/11 Pro Max")

case 1792:
print("iPhone XR/ 11 ")

default:
print("Unknown")
}
}

Objective-C:

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {
case 1136:
printf("iPhone 5 or 5S or 5C");
break;

case 1334:
printf("iPhone 6/6S/7/8");
break;

case 1920:
case 2208:
printf("iPhone 6+/6S+/7+/8+");
break;

case 2436:
printf("iPhone X/XS/11 Pro");
break;

case 2688:
printf("iPhone XS Max/11 Pro Max");
break;

case 1792:
printf("iPhone XR/ 11 ");
break;

default:
printf("Unknown");
break;
}
}

Xamarin.iOS:

if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1136) {
Console.WriteLine("iPhone 5 or 5S or 5C");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {
Console.WriteLine("iPhone 6/6S/7/8");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {
Console.WriteLine("iPhone 6+/6S+/7+/8+");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {
Console.WriteLine("iPhone X, XS, 11 Pro");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {
Console.WriteLine("iPhone XS Max, 11 Pro Max");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {
Console.WriteLine("iPhone XR, 11");
} else {
Console.WriteLine("Unknown");
}
}

Based on your question as follow:

Or use screenSize.height as float 812.0f not int 812.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// 812.0 on iPhone X, XS
// 896.0 on iPhone XS Max, XR.

if (screenSize.height >= 812.0f)
NSLog(@"iPhone X");
}

For more information you can refer the following page in iOS Human Interface Guidelines:

  • Adaptivity and Layout - Visual Design - iOS - Human Interface Guidelines

Swift:

Detect with topNotch:

If anyone considering using notch to detect iPhoneX, mind that on "landscape" its same for all iPhones.

var hasTopNotch: Bool {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.safeAreaInsets.top ?? 0 > 20
}else{
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}

return false
}

Objective-C:

- (BOOL)hasTopNotch {
if (@available(iOS 13.0, *)) {
return [self keyWindow].safeAreaInsets.top > 20.0;
}else{
return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
}
return NO;
}

- (UIWindow*)keyWindow {
UIWindow *foundWindow = nil;
NSArray *windows = [[UIApplication sharedApplication]windows];
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
foundWindow = window;
break;
}
}
return foundWindow;
}

UPDATE:

Do not use the userInterfaceIdiom property to identify the device type, as the documentation for userInterfaceIdiom explains:

For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.

That is, this property is just used to identify the running app's view style. However, the iPhone app (not the universal) could be installed in iPad device via App store, in that case, the userInterfaceIdiom will return the UIUserInterfaceIdiomPhone, too.

The right way is to get the machine name via uname. Check the following for details:

  • How to get device make and model on iOS?

iOS: Detect if the device is iPhone X family (frameless)

You could "fitler" for the top notch, something like:

var hasTopNotch: Bool {
if #available(iOS 11.0, tvOS 11.0, *) {
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}
return false
}

How to programmatically detect iPhone XS or iPhone X?

I use DeviceUtil to determine an iOS device's model.

According to this DeviceUtil GitHub post, the returned UIDevice hardwareString values are:

iPhone11,2 = iPhone XS
iPhone11,4 = iPhone XS Max
iPhone11,8 = iPhone XR

Curiously, the Xcode 10 GM Simulator's [UIScreen mainScreen].bounds.size returns 375 x 812 for iPhone X, XS, XS Max, and R devices. I had expected 414 x 896 for the XS Max and XR.

How can I detect if my device is an iPhoneX in Swift 4?

If you need to detect if a device is iPhoneX don't use bounds, it depends on the orientation of the device. So if the user opens your app in portrait mode it will fail. You can use Device property nativeBounds which doesn't change on rotation.

In iOS 8 and later, a screen’s bounds property takes the interface
orientation of the screen into account. This behavior means that the
bounds for a device in a portrait orientation may not be the same as
the bounds for the device in a landscape orientation. Apps that rely
on the screen dimensions can use the object in the
fixedCoordinateSpace property as a fixed point of reference for any
calculations they must make. (Prior to iOS 8, a screen’s bounds
rectangle always reflected the screen dimensions relative to a
portrait-up orientation. Rotating the device to a landscape or
upside-down orientation did not change the bounds.)

extension UIDevice {
var iPhoneX: Bool {
return UIScreen.main.nativeBounds.height == 2436
}
}

usage

if UIDevice.current.iPhoneX { 
print("This device is a iPhoneX")
}

Detect if the device is iPhone X

Based on your question, the answer is no. There are no direct methods. For more information you can get the information here:

  • How to get device make and model on iOS?

and

  • how to check screen size of iphone 4 and iphone 5 programmatically in swift

The iPhone X height is 2436 px

From Device Screen Sizes and resolutions:

Sample Image

From Device Screen Sizes and Orientations:

Sample Image

Swift 3 and later:

if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")

case 1334:
print("iPhone 6/6S/7/8")

case 1920, 2208:
print("iPhone 6+/6S+/7+/8+")

case 2436:
print("iPhone X/XS/11 Pro")

case 2688:
print("iPhone XS Max/11 Pro Max")

case 1792:
print("iPhone XR/ 11 ")

default:
print("Unknown")
}
}

Objective-C:

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {
case 1136:
printf("iPhone 5 or 5S or 5C");
break;

case 1334:
printf("iPhone 6/6S/7/8");
break;

case 1920:
case 2208:
printf("iPhone 6+/6S+/7+/8+");
break;

case 2436:
printf("iPhone X/XS/11 Pro");
break;

case 2688:
printf("iPhone XS Max/11 Pro Max");
break;

case 1792:
printf("iPhone XR/ 11 ");
break;

default:
printf("Unknown");
break;
}
}

Xamarin.iOS:

if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1136) {
Console.WriteLine("iPhone 5 or 5S or 5C");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {
Console.WriteLine("iPhone 6/6S/7/8");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {
Console.WriteLine("iPhone 6+/6S+/7+/8+");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {
Console.WriteLine("iPhone X, XS, 11 Pro");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {
Console.WriteLine("iPhone XS Max, 11 Pro Max");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {
Console.WriteLine("iPhone XR, 11");
} else {
Console.WriteLine("Unknown");
}
}

Based on your question as follow:

Or use screenSize.height as float 812.0f not int 812.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// 812.0 on iPhone X, XS
// 896.0 on iPhone XS Max, XR.

if (screenSize.height >= 812.0f)
NSLog(@"iPhone X");
}

For more information you can refer the following page in iOS Human Interface Guidelines:

  • Adaptivity and Layout - Visual Design - iOS - Human Interface Guidelines

Swift:

Detect with topNotch:

If anyone considering using notch to detect iPhoneX, mind that on "landscape" its same for all iPhones.

var hasTopNotch: Bool {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.safeAreaInsets.top ?? 0 > 20
}else{
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}

return false
}

Objective-C:

- (BOOL)hasTopNotch {
if (@available(iOS 13.0, *)) {
return [self keyWindow].safeAreaInsets.top > 20.0;
}else{
return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
}
return NO;
}

- (UIWindow*)keyWindow {
UIWindow *foundWindow = nil;
NSArray *windows = [[UIApplication sharedApplication]windows];
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
foundWindow = window;
break;
}
}
return foundWindow;
}

UPDATE:

Do not use the userInterfaceIdiom property to identify the device type, as the documentation for userInterfaceIdiom explains:

For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.

That is, this property is just used to identify the running app's view style. However, the iPhone app (not the universal) could be installed in iPad device via App store, in that case, the userInterfaceIdiom will return the UIUserInterfaceIdiomPhone, too.

The right way is to get the machine name via uname. Check the following for details:

  • How to get device make and model on iOS?

Detect if the device is iPhone X

Based on your question, the answer is no. There are no direct methods. For more information you can get the information here:

  • How to get device make and model on iOS?

and

  • how to check screen size of iphone 4 and iphone 5 programmatically in swift

The iPhone X height is 2436 px

From Device Screen Sizes and resolutions:

Sample Image

From Device Screen Sizes and Orientations:

Sample Image

Swift 3 and later:

if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 1136:
print("iPhone 5 or 5S or 5C")

case 1334:
print("iPhone 6/6S/7/8")

case 1920, 2208:
print("iPhone 6+/6S+/7+/8+")

case 2436:
print("iPhone X/XS/11 Pro")

case 2688:
print("iPhone XS Max/11 Pro Max")

case 1792:
print("iPhone XR/ 11 ")

default:
print("Unknown")
}
}

Objective-C:

if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
switch ((int)[[UIScreen mainScreen] nativeBounds].size.height) {
case 1136:
printf("iPhone 5 or 5S or 5C");
break;

case 1334:
printf("iPhone 6/6S/7/8");
break;

case 1920:
case 2208:
printf("iPhone 6+/6S+/7+/8+");
break;

case 2436:
printf("iPhone X/XS/11 Pro");
break;

case 2688:
printf("iPhone XS Max/11 Pro Max");
break;

case 1792:
printf("iPhone XR/ 11 ");
break;

default:
printf("Unknown");
break;
}
}

Xamarin.iOS:

if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone) {
if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1136) {
Console.WriteLine("iPhone 5 or 5S or 5C");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1334) {
Console.WriteLine("iPhone 6/6S/7/8");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1920 || (UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2208) {
Console.WriteLine("iPhone 6+/6S+/7+/8+");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2436) {
Console.WriteLine("iPhone X, XS, 11 Pro");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 2688) {
Console.WriteLine("iPhone XS Max, 11 Pro Max");
} else if ((UIScreen.MainScreen.Bounds.Height * UIScreen.MainScreen.Scale) == 1792) {
Console.WriteLine("iPhone XR, 11");
} else {
Console.WriteLine("Unknown");
}
}

Based on your question as follow:

Or use screenSize.height as float 812.0f not int 812.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
// 812.0 on iPhone X, XS
// 896.0 on iPhone XS Max, XR.

if (screenSize.height >= 812.0f)
NSLog(@"iPhone X");
}

For more information you can refer the following page in iOS Human Interface Guidelines:

  • Adaptivity and Layout - Visual Design - iOS - Human Interface Guidelines

Swift:

Detect with topNotch:

If anyone considering using notch to detect iPhoneX, mind that on "landscape" its same for all iPhones.

var hasTopNotch: Bool {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.safeAreaInsets.top ?? 0 > 20
}else{
return UIApplication.shared.delegate?.window??.safeAreaInsets.top ?? 0 > 20
}

return false
}

Objective-C:

- (BOOL)hasTopNotch {
if (@available(iOS 13.0, *)) {
return [self keyWindow].safeAreaInsets.top > 20.0;
}else{
return [[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0;
}
return NO;
}

- (UIWindow*)keyWindow {
UIWindow *foundWindow = nil;
NSArray *windows = [[UIApplication sharedApplication]windows];
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
foundWindow = window;
break;
}
}
return foundWindow;
}

UPDATE:

Do not use the userInterfaceIdiom property to identify the device type, as the documentation for userInterfaceIdiom explains:

For universal applications, you can use this property to tailor the behavior of your application for a specific type of device. For example, iPhone and iPad devices have different screen sizes, so you might want to create different views and controls based on the type of the current device.

That is, this property is just used to identify the running app's view style. However, the iPhone app (not the universal) could be installed in iPad device via App store, in that case, the userInterfaceIdiom will return the UIUserInterfaceIdiomPhone, too.

The right way is to get the machine name via uname. Check the following for details:

  • How to get device make and model on iOS?

Detect iPhone X with a macro

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)

iPhoneX and Notch detection

So I've come up with a method of detecting the iPhoneX with Javascript. My process also checks for the position of the Notch depending on the users device orientation:

https://codepen.io/marknotton/pen/NwpgBK

(function(window){

// Really basic check for the ios platform
// https://stackoverflow.com/questions/9038625/detect-if-device-is-ios
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

// Get the device pixel ratio
var ratio = window.devicePixelRatio || 1;

// Define the users device screen dimensions
var screen = {
width : window.screen.width * ratio,
height : window.screen.height * ratio
};

// iPhone X Detection
if (iOS && screen.width == 1125 && screen.height === 2436) {

// Set a global variable now we've determined the iPhoneX is true
window.iphoneX = true;

// Adds a listener for ios devices that checks for orientation changes.
window.addEventListener('orientationchange', update);
update();
}

// Each time the device orientation changes, run this update function
function update() {
notch();
iphoneXChecker();
}

// Notch position checker
function notch() {

var _notch = '';

if( 'orientation' in window ) {
// Mobile
if (window.orientation == 90) {
_notch = 'left';
} else if (window.orientation == -90) {
_notch = 'right';
}
} else if ( 'orientation' in window.screen ) {
// Webkit
if( screen.orientation.type === 'landscape-primary') {
_notch = 'left';
} else if( screen.orientation.type === 'landscape-secondary') {
_notch = 'right';
}
} else if( 'mozOrientation' in window.screen ) {
// Firefox
if( screen.mozOrientation === 'landscape-primary') {
_notch = 'left';
} else if( screen.mozOrientation === 'landscape-secondary') {
_notch = 'right';
}
}

window.notch = _notch;
}

})(window);

// Bespoke functions:
// The above functions have no jQuery Dependencies.
// The below code uses jQuery solely for this quick demo.
if ( window.iphoneX === true ) {
$('body').addClass('iphoneX');
}
function iphoneXChecker() {
if (window.notch == 'left') {
$('body').removeClass('notch-right').addClass('notch-left');
} else if (window.notch == 'right') {
$('body').removeClass('notch-left').addClass('notch-right');
} else {
$('body').removeClass('notch-right notch-left');
}
}

I can't help but feel like this is just a combination of little hacks. As you'll probably notice; my Javascript isn't exactly to a high standard and I'm sure there are better/cleaner ways to do this.

I'd be very happy to receive feedback and solutions to issues I've not considered.


If you just want to check for the iPhoneX (ignoring the Notch), this should do the job:

https://codepen.io/marknotton/pen/MOpodJ

(function(){

// Really basic check for the ios platform
// https://stackoverflow.com/questions/9038625/detect-if-device-is-ios
var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;

// Get the device pixel ratio
var ratio = window.devicePixelRatio || 1;

// Define the users device screen dimensions
var screen = {
width : window.screen.width * ratio,
height : window.screen.height * ratio
};

// iPhone X Detection
if (iOS && screen.width == 1125 && screen.height === 2436) {
alert('iPhoneX Detected!');
}
})();

How to detect if the device (iphone) has physical home button?

Check the safe area:

if @available(iOS 11.0, *), 
UIApplication.sharedApplication.keyWindow?.safeAreaInsets.bottom > 0 {
return true
}
return false

Swift 4.2 version:-

var isBottom: Bool {
if #available(iOS 11.0, *), let keyWindow = UIApplication.shared.keyWindow, keyWindow.safeAreaInsets.bottom > 0 {
return true
}
return false
}

You can also check the device type (check out this post), but checking the safe area is probably the easiest way.



Related Topics



Leave a reply



Submit