端的に言えばUIDeviceにHardwareカテゴリとして以下のようなクラスを実装すると簡単に得ることができます。
UIDevice+Hardware.h
#include <sys/sysctl.h>
@interface UIDevice (Hardware) - (NSString *) platform;
- (NSString *) platformString;
@end
UIDevice+Hardware.m
#import "UIDevice+Hardware.h"
@implementation UIDevice (Hardware) - (NSString *) platform { size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0); char *machine = malloc(size); sysctlbyname("hw.machine", machine, &size, NULL, 0);
/*
Platforms iPhone1,1 -> iPhone 2G iPhone1,2 -> iPhone 3G iPhone2,1 -> iPhone 3GS iPhone3,1 -> iPhone 4 GSM iPhone3,3 -> iPhone 4 CDMA iPhone4,1 -> iPhone 4S iPod1,1 -> iPod touch 1G iPod2,1 -> iPod touch 2G iPod3,1 -> iPod touch 3G iPod4,1 -> iPod touch 4G iPad1,1 -> iPad (Wifi or GSM) iPad2,1 -> iPad 2 Wifi iPad2,2 -> iPad 2 GSM iPad2,3 -> iPad 2 CDMA */
NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; free(machine);
return platform; }
- (NSString *) platformString
{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone";
else if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
else if ([platform hasPrefix:@"iPhone2,"]) return @"iPhone 3GS";
else if ([platform hasPrefix:@"iPhone3,"]) return @"iPhone 4";
else if ([platform hasPrefix:@"iPhone4,"]) return @"iPhone 4S";
else if ([platform hasPrefix:@"iPod1,"]) return @"iPod touch 1G";
else if ([platform hasPrefix:@"iPod2,"]) return @"iPod touch 2G";
else if ([platform hasPrefix:@"iPod3,"]) return @"iPod touch 3G";
else if ([platform hasPrefix:@"iPod4,"]) return @"iPod touch 4G";
else if ([platform hasPrefix:@"iPad1,"]) return @"iPad";
else if ([platform hasPrefix:@"iPad2,"]) return @"iPad 2"; if ([platform hasSuffix:@"86"] || [platform isEqualToString:@"x86_64"]) { BOOL smallerScreen = [[UIScreen mainScreen] bounds].size.width < 768; return smallerScreen ? @"Simulator iPhone" : @"Simulator iPad"; } else return @"Unknown Device";
}
@end
使い方の例
#import "UIDevice+Hardware.h"
// 中略
if ([[[UIDevice currentDevice] platformString]
isEqualToString:@"iPhone 4S"]) {
// iPhone 4Sの場合の処理
}
参考文献
erica/uidevice-extension - GitHub
https://github.com/erica/uidevice-extension
UIDevice iphone 4/4S - iPhone Dev SDK Forum
http://www.iphonedevsdk.com/forum/iphone-sdk-development/96887-uidevice-iphone-4-4s.html
"iPhone3,3" が iPhone 4 CDMA、
"iPhone4,1" が iPhone 4S、
などの文字列とデバイスの対応表は
どこから得ました?
アップル公式の完全なリストなどがあれば
一番うれしいのですが、なさそうですね…。
p.s.
調べた限りではここのブログが一番詳しかったです。