Created
November 2, 2012 18:38
-
-
Save jsumners/4003438 to your computer and use it in GitHub Desktop.
A category on UIApplication for determining the iPhone device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 [email protected] has been added to the project. | |
*/ | |
- (BOOL)isIphone5; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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