Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Changing code depending on device

I would like the user to be able to click a button and if the device is an iPhone 5 a label will display "This is an iPhone 5" but if the device is an iPhone 4/4s the label will say that instead. Thanks

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

You can define the following macros at the very top of your view controller (after your import statements).

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)

And then somewhere in your code do the following:

NSString *text;

if ( IS_IPHONE_5 ) 
    text = @"This is an iPhone 5";
else
    text = @"This is an iPhone 4/4S";

Thanks