在游戏开发中,我们常常需要获取iOS设备信息来进行数据统计和性能适配。
比如在数据统计时,需要获取当前设备的名称,类型,系统名称,系统版本,UUID等,这些都可以通过UIDevice类来获取。代码如下:
NSString *deviceName = [[UIDevice currentDevice] name]; // 设备名称,如YIYI的iPhone
NSString *deviceModel = [[UIDevice currentDevice] model]; // 设备类型,如 iPad
NSString *osName = [[UIDevice currentDevice] systemName]; // 系统名称,如 iOS
NSString *osVersion = [[UIDevice currentDevice] systemVersion]; // 系统版本,如12.3
NSString *uuid = [[NSUUID UUID] UUIDString]; // UUID 是 Universally Unique Identifier 的缩写,中文意思是通用唯一识别码.
在性能适配时,我们往往要建立一张机型配置表,指定游戏在特定机型上的性能配置参数,从而使高中低机型的玩家都获得相对最好的游玩体验。这就需要获取设备的机型信息,但Apple并没有提供直接的API获取,不过由于iOS是基于UNIX改进的,所以我们可以通过UNIX中放置系统硬件配置信息的struct utsname
类的machine
属性来获取设备标签,然后从http://theiphonewiki.com/wiki/Models获取最新的设备标签,机型型号的对照表,再通过这种映射关系获取相应的设备型号。代码如下:
#import "sys/utsname.h”
+ (NSString *)deviceName {
struct utsname systemInfo;
uname(&systemInfo);
NSString *internalName = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
NSDictionary *dict = [
// iPhone
@"iPhone1,1" : @"iPhone",
@"iPhone1,2" : @"iPhone 3G",
@"iPhone2,1" : @"iPhone 3GS",
@"iPhone3,1" : @"iPhone 4",
@