Updated 8/18/09: Added info for 3GS (Thanks Jamey).
Erica Sadun recently shared a great post on Ars Technica showing how to determine specifically which model of iPhone or iPod Touch is being used, as opposed to the high-level “iPhone vs. iPod Touch” information made available via the UIDevice -model: method. As she points out, knowing the exact model can be pretty helpful as there are some significant differences between the different devices (e.g., the second-generation iPod Touch has speakers, while the first-generation model does not).
In a nutshell, you use a C function called sysctlbyname to get the hardware model (and if you’re not familiar with doing stuff in C like manually allocating and freeing memory, the code sample is really helpful). Here’s a snippet from the original post:
- (NSString *) platform { size_t size; sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0); /* Possible values: "iPhone1,1" = iPhone 1G "iPhone1,2" = iPhone 3G "iPhone2,1" = iPhone 3GS "iPod1,1" = iPod touch 1G "iPod2,1" = iPod touch 2G */ NSString *platform = [NSString stringWithCString:machine]; free(machine); return platform; }