public
Last active

A category on UIApplication for determining the iPhone device

  • Download Gist
UIApplication+iPhoneVersion.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#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
UIApplication+iPhoneVersion.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.