NSString *modelname = [[UIDevice currentDevice]model];
if ([modelname isEqualToString:@"iPhone"]) {
// iPhone
}
if ([modelname isEqualToString:@"IPod Touch"]) {
// iPod touch
}
if ([modelname isEqualToString:@"iPhone Simulator"]) {
// iPhone Simulator
}
#import <TargetConditionals.h>
#if TARGET_OS_IPHONE
// iPhone Device
#endif
#if TARGET_IPHONE_SIMULATOR
// iPhone Simulator
#endif
#if !TARGET_IPHONE_SIMULATOR
// iPhone Device
#endif
可以通过 uname 函数取得当前机器的版本。
struct utsname u;
uname(&u);
NSString *machine = [NSString stringWithCString:u.machine];
if ([machine isEqualToString:@"iPhone1,1"]) {
// iPhone 1G
}
if ([machine isEqualToString:@"iPhone1,2"]) {
// iPhone 3G
}
if ([machine isEqualToString:@"iPhone2,1"]) {
// iPhone 3GS
}
if ([machine isEqualToString:@"iPod1,1"]) {
// iPod touch 1G
}
if ([machine isEqualToString:@"iPod2,1"]) {
// iPod touch 2G
}
if ([machine isEqualToString:@"iPod3,1"]) {
// iPod touch Late2009
}
或者通过 sysctlbyname() 函数取得:
- (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;
}
使用 UIDevice 的属性 systemVersion 来得到
NSString *osversion = [UIDevice currentDevice].systemVersion;
if ([osversion isEqualToString:@"2.1"]) {
// iPhone
}
if ([osversion isEqualToString:@"2.2.1"]) {
// iPod touch
}
if ([osversion isEqualToString:@"3.0"]) {
// iPhone Simulator
}
iPhone SDK 版本宏
// 当前系统支持的最小版本
__IPHONE_OS_VERSION_MIN_REQUIRED
// 当前系统支持的最大版本
__IPHONE_OS_VERSION_MAX_ALLOWED
比如用 iPhone OS SDK 3.1.2 编译的程序
__IPHONE_OS_VERSION_MIN_REQUIRED == __IPHONE_3_0
__IPHONE_OS_VERSION_MAX_ALLOWED == __IPHONE_3_1
可以在程序中使用下面类似的 $ifdef 语句
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_2_2
// iPhone OS SDK 3.0 以后版本的处理
#else
// iPhone OS SDK 3.0 之前版本的处理
#endif
iPhone OS SDK 4 推出的时候,可以
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_2_2
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_3_1
// iPhone OS SDK 4.0 以后版本的处理
#else
// iPhone OS SDK 3.0 ~ 4.0 版本的处理
#endif
#else
// iPhone OS SDK 3.0 之前版本的处理
#endif