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

Solutions Collect From Internet About “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")
}