本来这是QQ群里一个朋友的问题,网上查了一下,现在将方法大概整理一下,以便于需要的朋友使用。当然,使用
UIDevice类,也可以简单的获得到底当前平台是iphone 还是itouch,但是你所获得的信息也就仅限于此了,如果想获得更多的
硬件平台信息呢?使用sysctlbyname,即可。
获取当前的平台信息,需要查询hw.machine 值,返回值为以下字符串:
iPhone1,1 (iphone一代)
iPhone1,2 (iphone 3G)
iPhone2,1 (iphone 3Gs)
iPhone3,1 (Iphone 4)
iPod1,1 (iPod Touch 1代)
iPod2,1 (iPod Touch 2代)
iPod3,1 (iPod Touch 3代)
i386 (模拟器)
使用方法:
UIDeviceHardware *h=[[UIDeviceHardware alloc] init];
[self setDeviceModel:[h platformString]];
[h release];
UIDeviceHardware.h 头文件
//
// UIDeviceHardware.h
//
// Used to determine EXACT version of device software is running on.
#import <Foundation/Foundation.h>
@interface UIDeviceHardware : NSObject
- (NSString *) platform;
- (NSString *) platformString;
@end
UIDeviceHardware.m 实现文件
//
// UIDeviceHardware.m
//
// Used to determine EXACT version of device software is running on.
#import "UIDeviceHardware.h"
#include <sys/types.h>
#include <sys/sysctl.h>
@implementation UIDeviceHardware
- (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];
free(machine);
return platform;
}
- (NSString *) platformString{
NSString *platform = [self platform];
if ([platform isEqualToString:@"iPhone1,1"]) return @"iPhone 1G";
if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";
if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3GS";
if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";
if ([platform isEqualToString:@"iPod1,1"]) return @"iPod Touch 1G";
if ([platform isEqualToString:@"iPod2,1"]) return @"iPod Touch 2G";
if ([platform isEqualToString:@"iPod3,1"]) return @"iPod Touch 3G";
if ([platform isEqualToString:@"i386"]) return @"iPhone Simulator";
return platform;
}
@end