iPadだったらこうするiPhoneだったらこうする。はたまた、iPhone 4は良いがiPhone 3GとiPod touch 2Gはメモリが少ないのでこの機能をドロップするなど、端末のモデルによってあれしたいこれしたいという事が多々あると思います。
端末情報を取得する方法についてご紹介したいと思います。
iOSのシステムバージョンを取得する
iOSのシステムバージョンを取得するには、UIDeviceクラスのsystemVersionプロパティを使用します。
UIDevice* currentDevice = [UIDevice currentDevice]; NSString* sysVersion = [currentDevice systemVersion];
モデル(機種名)を取得する
モデル(機種名)を文字列で取得することができます。UIDeviceクラスのmodelプロパティを使用します。
UIDevice* currentDevice = [UIDevice currentDevice]; NSString* model = [currentDevice model];
modelプロパティが返す値は、下記の通りです。
| 取得出来るモデル(機種名) |
|---|
| iPhone |
| iPod touch |
| iPad |
| iPhone Simulator |
プラットフォームを取得する
sysctlbyname関数でプラットフォーム情報を取得することができます。
#include <sys/types.h> #include <sys/sysctl.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); NSString *platform = [NSString stringWithCString:machine encoding:NSUTF8StringEncoding]; free(machine); return platform; }
取得したプラットフォーム情報はiPhone6,1という形のいわゆるプラットフォームコードになっています。プラットフォームコードと実際に販売されている製品名とのマッピングリストを以下にまとめています。
| プラットフォームコード | 製品名 |
|---|---|
| iPhone1,1 | iPhone 1G |
| iPhone1,2 | iPhone 3G |
| iPhone2,1 | iPhone 3GS |
| iPhone3,1 | iPhone 4 (GSM) |
| iPhone3,2 | iPhone 4 (GSM Rev A) |
| iPhone3,3 | iPhone 4 (CDMA) |
| iPhone4,1 | iPhone 4S |
| iPhone5,1 | iPhone 5 (GSM) |
| iPhone5,2 | iPhone 5 (Global) |
| iPhone5,3 | iPhone 5c (GSM) |
| iPhone5,4 | iPhone 5c (Global) |
| iPhone6,1 | iPhone 5s (GSM) |
| iPhone6,2 | iPhone 5s (Global) |
| iPad1,1 | iPad 1G |
| iPad2,1 | iPad 2 (WiFi) |
| iPad2,2 | iPad 2 (GSM) |
| iPad2,3 | iPad 2 (CDMA) |
| iPad2,4 | iPad 2 (Rev A) |
| iPad3,1 | iPad 3 (WiFi) |
| iPad3,2 | iPad 3 (GSM) |
| iPad3,3 | iPad 3 (Global) |
| iPad3,4 | iPad 4 (WiFi) |
| iPad3,5 | iPad 4 (GSM) |
| iPad3,6 | iPad 4 (Global) |
| iPad4,1 | iPad Air (WiFi) |
| iPad4,2 | iPad Air (Cellular) |
| iPad2,5 | iPad mini 1G (WiFi) |
| iPad2,6 | iPad mini 1G (GSM) |
| iPad2,7 | iPad mini 1G (Global) |
| iPad4,4 | iPad mini 2G (WiFi) |
| iPad4,5 | iPad mini 2G (Cellular) |
| iPod1,1 | iPod touch 1G |
| iPod2,1 | iPod touch 2G |
| iPod3,1 | iPod touch 3G |
| iPod4,1 | iPod touch 4G |
| iPod5,1 | iPod touch 5G |
| AppleTV2,1 | AppleTV 2 |
| i386 | iPhone Simulator |
| x86_64 | iPhone Simulator |