iOS Tips and Tricks - Detecting Device and OS

When writing iOS apps it sometimes becomes necessary to detect what device or OS you're application is currently running on. In this post i'll give you quick access to snippets to do just that. 

Device Detection

iPad

BOOL iPad = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);

iPhone

BOOL iPhone = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone);

iPhone 5 (4" screen)

BOOL iPhone5 = ([ [ UIScreen mainScreen ] bounds ].size.height >= 568 );

Retina Display

BOOL retina =   ([UIScreen mainScreen].scale >= 2);

OS Detection

iOS 7

BOOL iOS7 = (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1);

It's worth noting that you can also detect the OS by validating against API that you know only exists in a specific version of the OS:

BOOL iOS7 = NSClassFromString(@"UIInterpolatingMotionEffect") != nil;

Enjoy!

By Stephen Zaharuk (SteveZ)


Tags / ,