public
Last active

Detect specific iPhone/iPod/iPad model

  • Download Gist
UIDevice+Model.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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
UIDevice+Model.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
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.