I need a macro that determines whether the device is an iPhone 4, or an iPhone 5. I was told that there is a macro that can do this. Does anyone know what it is or where I can find it?

- 5,753
- 72
- 57
- 129

- 2,646
- 6
- 32
- 45
-
2you cannot use a macro for that, you need to check at runtime – Ultrakorne Mar 13 '13 at 16:58
4 Answers
I found one that does the trick
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double) 568) < DBL_EPSILON)

- 2,646
- 6
- 32
- 45
Unfortunately not only is there not a macro, there isn't even a public API call that returns the device type to the degree it can distinguish between the likes of different versions of iPhone (e.g. iPhone 4 & iPhone5).
Device Detection
You can use the UI_USER_INTERFACE_IDIOM macro to determine whether the user is running on an iPhone/iPod or an iPad:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// The device is an iPad running iOS 3.2 or later.
}
else {
// The device is an iPhone or iPod touch.
}
Taken from the 'Advanced App Tricks section of the iOS App Programming Guide.
Screen Size Detection
In relation to supporting multiple screen sizes and the iPhone 4 and iPhone 5 universally in particular the iOS App Programming Guide states:
To support the larger screen in your code properly, never make assumptions about the current device’s screen size. Instead, always retrieve the size of a screen, window, or view dynamically and use that size information to configure your interface. You should also build your user interface using view-based constraints, which make it much easier to manage the changes to your view hierarchies at runtime.
iOS Version Detection
If it isn't the screen size your looking to detect but the version of iOS the user is running Apple recommends checking for specific functionality and using a fallback if it isn't available. Again, taken from the iOS App Programming Guide:
To determine whether a method is available on an existing class, use the instancesRespondToSelector: class method or the respondsToSelector: instance method.
Apps that link against iOS SDK 4.2 and later can use the weak linking support introduced in that version of the SDK. This support lets you check for the existence of a given Class object to determine whether you can use that class.
Apps that link against iOS SDK 4.1 and earlier must use the NSClassFromString function to see whether a class is defined. If the function returns a value other than nil, you may use the class.
To determine whether a C-based function is available, perform a Boolean comparison of the function name to NULL. If the symbol is not NULL, you can use the function.
Finally, yasirmturk provides a series of convenience macros here for checking the version of iOS the user is running running.
This is what I do:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// iPad
} else {
if (CGRectGetHeight([UIScreen mainScreen].bounds) >= 568.0) {
// 4 inch - iPhone 5 or the new iPod touch
}
else {
// 3,5 inch - iPhone 4S and older
}
}

- 26,549
- 8
- 62
- 79
-
You can also call `[UIScreen mainScreen].scale` to determine if the device is a retina display or not, giving more device types. – colincameron Mar 13 '13 at 17:18
-
@c.cam108 Yes, but this will be true for iPhone 4/4S, iPhone 5 and the iPad. My code is used to differentiate them. – Adam Mar 13 '13 at 17:22
I made a category on UIDevice to implement this. Pretty simple implementation but the code is
The .h
#import <UIKit/UIKit.h>
@interface UIDevice (Recognition)
- (BOOL)isDeviceiPhone5;
@end
The .m #import "UIDevice+Recognition.h"
@implementation UIDevice (Recognition)
- (BOOL)isDeviceiPhone5
{
return ([UIScreen mainScreen].bounds.size.height == 568.0f);
}
@end
Then from anywhere you can just do [[UIDevice currentDevice] isDeviceiPhone5]

- 1,413
- 2
- 12
- 23
-
Why don't you just do: `return [UIScreen mainScreen].bounds.size.height == 568.0f;`? – Adam Mar 13 '13 at 17:32
-