Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.