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 variant of iOS device sizes.

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

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

 

if self.view.frame.size.width == 320 {
    // It's 3.5 or 4 Inch iPhone, iPhone 4,iPhone 4s, iPhone 5, iPhone 5s
    // Your code here
} else if self.view.frame.size.width == 375 {
    // It's 4.7 Inch iPhone, iPhone 6, iPhone 7
    // Your code here
} else if self.view.frame.size.width == 414 {
    // It's 5.5 Inch iPhone, iPhone 6 Plus, iPhone 7 Plus
    // Your code here
} else if self.view.frame.size.width == 768 {
    // It's 9.7 inch iPad
    // Your code here
} else if self.view.frame.size.width == 1024 {
    // It's 12.9 Inch iPad
    // Your code here
} else {
    // Device is different
    // Your code here
}

 
Here is another take at situation by solving this problem by identifying device height property,

 

if self.view.frame.size.height == 480 {
    // It's 3.5 inch iPhone
    // Your code here
} else if self.view.frame.size.height == 568 {
    // It's 4 inch iPhone
    // Your code here
} else if self.view.frame.size.height == 667 {
    // It's 4.7 Inch iPhone, iPhone 6, iPhone 7
    // Your code here
} else if self.view.frame.size.height == 736 {
    // It's 5.5 Inch iPhone, iPhone 6 Plus, iPhone 7 Plus
    // Your code here
} else if self.view.frame.size.height == 1024 {
    // It's 9.7 inch iPad
    // Your code here
} else if self.view.frame.size.height == 1366 {
    // It's 12.9 Inch iPad
    // Your code here
} else {
    // Device is different
    // 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 *