Skip to content

Instantly share code, notes, and snippets.

@zoul
Created March 20, 2010 10:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoul/338600 to your computer and use it in GitHub Desktop.
Save zoul/338600 to your computer and use it in GitHub Desktop.
Detect specific iPhone/iPod/iPad model
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
#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