Created
March 20, 2010 10:14
-
-
Save zoul/338600 to your computer and use it in GitHub Desktop.
Detect specific iPhone/iPod/iPad model
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
typedef enum { | |
kDeviceTypeUnknown = 0, | |
kDeviceTypeSimulator, | |
kDeviceTypeiPhone1G, | |
kDeviceTypeiPhone3G, | |
kDeviceTypeiPhone3GS, | |
kDeviceTypeiPhone4, | |
kDeviceTypeiPod1G, | |
kDeviceTypeiPod2G, | |
kDeviceTypeiPad | |
} DeviceType; | |
@interface UIDevice (Model) | |
@property(readonly) NSString *platformID; | |
@property(readonly) DeviceType deviceType; | |
@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 "UIDevice+Model.h" | |
#import <sys/types.h> | |
#import <sys/sysctl.h> | |
#define BOX(x) [NSNumber numberWithInt:x] | |
@implementation UIDevice (Model) | |
- (NSString*) platformID | |
{ | |
size_t size; | |
sysctlbyname("hw.machine", NULL, &size, NULL, 0); | |
char *machine = malloc(size); | |
sysctlbyname("hw.machine", machine, &size, NULL, 0); | |
NSString *platform = [NSString stringWithUTF8String:machine]; | |
free(machine); | |
return platform; | |
} | |
- (DeviceType) deviceType | |
{ | |
NSDictionary *tagMap = [NSDictionary dictionaryWithObjectsAndKeys: | |
BOX(kDeviceTypeSimulator), @"i386", | |
BOX(kDeviceTypeiPhone1G), @"iPhone1,1", | |
BOX(kDeviceTypeiPhone3G), @"iPhone1,2", | |
BOX(kDeviceTypeiPhone3GS), @"iPhone2,1", | |
BOX(kDeviceTypeiPod1G), @"iPod1,1", | |
BOX(kDeviceTypeiPod2G), @"iPod2,1", | |
BOX(kDeviceTypeiPad), @"iPad1,1", | |
BOX(kDeviceTypeiPhone4), @"iPhone3,1", | |
nil]; | |
return [[tagMap objectForKey:self.platformID] intValue]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment