2010-07-25

iPhoneアプリ開発: iPhone 4と3GSと3GやiPod touchの世代を判別する

iPhone 4と3GSと3GやiPod touchの世代を判別する方法です。

iPhone 3Gと3GSやiPod touchの世代を判別する方法 - 24/7 twenty-four seven のソースコードを参考に、iPhone4 用に更新して、
必要なヘッダファイルを追加して、
deprecatedなメソッド( NSString の stringWithCString メソッド)を修正した
だけのコードです。


#include <sys/sysctl.h>
/*
Return device model name.
Possible values:
"iPhone1,1" = iPhone 1G
"iPhone1,2" = iPhone 3G
"iPhone2,1" = iPhone 3GS
"iPhone3,1" = iPhone 4
"iPhone3,3" = iPhone 4 (CDMA)
"iPhone4,1" = iPhone 4S
"iPhone5,1" = iPhone 5
"iPod1,1" = iPod touch 1G
"iPod2,1" = iPod touch 2G
"iPod3,1" = iPod touch 3G
"iPod4,1" = iPod touch 4G
"iPad1,1" = iPod
"iPad2,1" = iPod 2 (WiFi)
"iPad2,2" = iPod 2 (GSM)
"iPad2,3" = iPod 2 (CDMAV)
"iPad2,4" = iPod 2 (CDMAS)
"iPad3,1" = iPod 3
"iPad3,2" = iPod 3 (GSM+CDMA)
"iPad3,3" = iPod 3 (GSM)
"AppleTV2,1" = Apple TV (2G)
"AppleTV3,1" = Apple TV (3G)
"i386" = Simulator
"x86_64" = Simulator
*/
- (NSString *)machineName
{
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char *tmpMachine = malloc(size);
sysctlbyname("hw.machine", tmpMachine, &size, NULL, 0);
NSString *machine = [NSString stringWithCString:tmpMachine encoding: NSUTF8StringEncoding];
free(tmpMachine);
return machine;
}
- (BOOL)isIphone4
{
return [[self machineName] hasPrefix:@"iPhone3,"];
}
view raw gistfile1.m hosted with ❤ by GitHub

参考:

0 件のコメント: