How to programmatically detect iPhone XS or iPhone X?

Andrei Herford picture Andrei Herford · Sep 13, 2018 · Viewed 18.5k times · Source

One of my apps connects to a web app service that delivers device specific news to to the user. To adapt this to the latest iPhone versions I need to programmatically distinguish between the iPhone XS and iPhone X. How can this be done?

[[UIScreen mainScreen] bounds].size was always a good starting point to tell the different devices apart. However, iPhone XS and iPhone X have the same screen dimensions: 1125 x 2436. Thus using [[UIScreen mainScreen] bounds].size does not work in this case.

Is there any other method to detect the latest iPhone versions?

EDIT: This is NOT a duplicate of the question "How to detected iPhone X" since all answers/solutions discussed there use the screen dimensions to specify the device type. As described above iPhone X and iPhone XS have the exact same dimensions and thus the solutions cannot tell these two apart...

Answer

Umair picture Umair · Sep 20, 2018

You can determine using UIScreen.main.nativeBounds.

 if UIDevice().userInterfaceIdiom == .phone {
        switch UIScreen.main.nativeBounds.height {
        case 1136:
            print("IPHONE 5,5S,5C")
        case 1334:
            print("IPHONE 6,7,8 IPHONE 6S,7S,8S ")
        case 1920, 2208:
            print("IPHONE 6PLUS, 6SPLUS, 7PLUS, 8PLUS")
        case 2436:
            print("IPHONE X, IPHONE XS, IPHONE 11 PRO")
        case 2688:
            print("IPHONE XS MAX, IPHONE 11 PRO MAX")
        case 1792:
            print("IPHONE XR, IPHONE 11")
        default:
            print("UNDETERMINED")
        }
    }

.nativeBounds won't change even if the orientation is changed. https://developer.apple.com/documentation/uikit/uiscreen/1617810-nativebounds