How to identify user is on iPhone, iPad or iPod touch by accessing their device width property. In other words how to write conditional code for 3.5 inch iPhone, 4 Inch iPhone, 4.7 inch iPhone, 5.5 inch iPhone, 9.7 inch iPad, 12.9 inch iPad

You may be programming Universal App for iPhone, iPad, iPad Pro & iPod touch. And you may be wondering how to write conditional code for that variant iOS device sizes.

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

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

 

Note: This code uses native bounds of device so this code 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 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 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
}

 
Hope this solves your problem,

 
Thanks & Regards
Mandar Apte

Leave a Reply

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