Skip to content

Instantly share code, notes, and snippets.

@jsumners
Created November 2, 2012 18:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsumners/4003438 to your computer and use it in GitHub Desktop.
Save jsumners/4003438 to your computer and use it in GitHub Desktop.
A category on UIApplication for determining the iPhone device
#import <UIKit/UIKit.h>
#define IS_IPHONE (!IS_IPAD)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPhone)
/**
Provides a few methods to determine what sort of device the application is
running on. Specifically, the methods make it easy to determine what sort of
iPhone resolution is available: `[[UIApplication sharedApplication] isIphone3]`
*/
@interface UIApplication (iPhoneVersion)
/** Returns YES if the device is only capable of 320x480. */
- (BOOL)isIphone3;
/** Returns YES if the device's native resolution is 640x960. */
- (BOOL)isIphone4;
/**
Returns YES if the devices's native resolution is 640x1136. Well, not quite.
Even if the native resolution is 640x1136, this method will only return YES
if a Default-568h@2x.png has been added to the project.
*/
- (BOOL)isIphone5;
@end
#import "UIApplication+iPhoneVersion.h"
@implementation UIApplication (iPhoneVersion)
/**
A private function to reduce some typing.
*/
static bool screenMatchesSize(int x, int y) {
return CGSizeEqualToSize([[UIScreen mainScreen] preferredMode].size,
CGSizeMake(x, y));
}
/**
A private function to reduce redundancy.
*/
static bool isPhoneVersion(int phoneVersion) {
if (!IS_IPHONE) {
return NO;
}
BOOL result = NO;
switch (phoneVersion) {
case 3:
result = screenMatchesSize(320, 480);
break;
case 4:
result = screenMatchesSize(640, 960);
break;
case 5:
result = screenMatchesSize(640, 1136);
break;
default:
break;
}
return result;
}
- (BOOL)isIphone3
{
return isPhoneVersion(3);
}
- (BOOL)isIphone4
{
return isPhoneVersion(4);
}
- (BOOL)isIphone5
{
return isPhoneVersion(5);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment