Last active
December 25, 2015 23:39
-
-
Save d3signerd/7058156 to your computer and use it in GitHub Desktop.
iOS Device + Model
An iOS Category that will provide the device model that works in iOS7 for iPhones, iPads, and iPods.
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
// | |
// UIDevice+model.h | |
// | |
// Created by Kellen Styler on 10/18/13. | |
// | |
#import <UIKit/UIKit.h> | |
#define DEVICE_IPAD @"iPad" | |
#define DEVICE_IPOD @"iPod" | |
#define DEVICE_IPHONE @"iPhone" | |
#define DEVICE_SIM @"Simulator" | |
@interface UIDevice (model) | |
// Instance Methods | |
+ (BOOL) isIPhone; | |
+ (BOOL) isIPhone5; | |
+ (BOOL) isIPad; | |
+ (BOOL) isIPod; | |
+ (BOOL) isRetina; | |
+ (NSString *) modelType; | |
+ (NSString *) modelTypeFull; | |
@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
// | |
// UIDevice+model.m | |
// | |
// Created by Kellen Styler on 10/18/13. | |
// | |
#import "UIDevice+model.h" | |
@implementation UIDevice (model) | |
#pragma mark - Instance Methods | |
+ (BOOL) isIPhone | |
{ return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone; } | |
+ (BOOL) isIPhone5 | |
{ return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && [UIDevice isRetina ]; } | |
+ (BOOL) isIPad | |
{ return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad; } | |
+ (BOOL) isIPod | |
{ return [[UIDevice modelType ] isEqualToString:DEVICE_IPOD ]; } | |
+ (BOOL) isRetina | |
{ return [[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0; } | |
+ (NSString *) modelType | |
{ | |
// Get deveice type | |
size_t size; | |
sysctlbyname( "hw.machine" , NULL , &size , NULL , 0 ); | |
char * answer = malloc( size ); | |
sysctlbyname( "hw.machine" , answer, &size , NULL , 0) ; | |
NSString * result = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding ]; | |
free( answer ); | |
// Check for iPhone | |
if( [result rangeOfString:DEVICE_IPHONE ].location != NSNotFound ) | |
return DEVICE_IPHONE; | |
else if( [result rangeOfString:DEVICE_IPAD ].location != NSNotFound ) | |
return DEVICE_IPAD; | |
else if( [result rangeOfString:DEVICE_IPOD ].location != NSNotFound ) | |
return DEVICE_IPOD; | |
else | |
return DEVICE_SIM; | |
return result; | |
} | |
+ (NSString *) modelTypeFull | |
{ | |
// Get deveice type | |
size_t size; | |
sysctlbyname( "hw.machine" , NULL , &size , NULL , 0 ); | |
char * answer = malloc( size ); | |
sysctlbyname( "hw.machine" , answer, &size , NULL , 0) ; | |
NSString * result = [NSString stringWithCString:answer encoding: NSUTF8StringEncoding ]; | |
free( answer ); | |
return result; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment