Identify the iPhone or iPad model programmatically using Swift Language.

You may be programming a Universal App for iPhone, iPad, or iPad Pro and wondering how to write conditional code for those variant iOS device sizes.

So here is how to write code by identifying the device height of your display or frame.

Here is how to solve this situation by identifying device height,

Note:
This code uses the native bounds of the device, so it will work for all current devices that may be in landscape or portrait mode. Code is written in Swift Programming Language.

Here is how to identify the iPad Model:

print(UIScreen.main.nativeBounds.height)
switch UIScreen.main.nativeBounds.height {
case 2048:
    print("iPad 5th Generation, iPad Retina, iPad Air, iPad Pro 9.7")
    // Your code here
case 2224:
    print("iPad Pro 10.5")
    // Your code here
case 2732:
    print("iPad Pro 12.9")
    // Your code here
default:
    print("iPad unknown")
    // Your code here
}

Here is how to identify the iPhone Model:

print(UIScreen.main.nativeBounds.height)
switch UIScreen.main.nativeBounds.height {
case 480:
    print("iPhone Classic")
    // Your code here
case 960:
    print("iPhone 4 or 4S")
    // Your code here
case 1136:
    print("iPhone 5 or 5S or 5C")
    // Your code here
case 1334:
    print("iPhone 6 or 6S")
    // Your code here
case 2208:
    print("iPhone 6+ or 6S+")
    // Your code here
case 2436:
    print("iPhone X")
    // Your code here
default:
    print("unknown")
    // Your code here
}

I hope this solves your problem,

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.