如何获得ios 中的硬件信息(上)

来源:互联网 发布:centos打开端口命令 编辑:程序博客网 时间:2024/05/12 15:13

     ios 自身提供了UIDevice 的类给与我们获得一些硬件的属性。我们还可以通过一些其他的方法得到更多的信息。

一。能够直接通过UIDevice得到的属性有:

The device unique identifier
The name of the device
The localized version of the model of the device (iPhone, iPod Touch)
The system name (iPhone OS)
The system version (2.2.1, etc.)
The language and locale used on the device(en_US)
The short version of the time zone (MST)

实现的代码如下:

UIDevice *aDevice = [UIDevice currentDevice];
NSLocale *currentLocale = [NSLocale currentLocale];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
 
NSMutableArray *passingArray = [[NSMutableArray alloc] initWithCapacity:5];
[passingArray addObject: [aDevice uniqueIdentifier]];
[passingArray addObject: [aDevice name]];
[passingArray addObject: [aDevice localizedModel]];
[passingArray addObject: [aDevice systemName]];
[passingArray addObject: [aDevice systemVersion]];
[passingArray addObject: [defs objectForKey:@"AppleLocale"]];
NSString *timeZoneString = [timeZone localizedName:NSTimeZoneNameStyleShortStandard locale:currentLocale];
if([timeZone isDaylightSavingTimeForDate:[NSDate date]]){
timeZoneString = [timeZone localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:currentLocale];
}
[passingArray addObject: timeZoneString];
The JavaScript to display this information, found in the functions.js file of the DeviceDescriptionExample, is seen here.
function displayDeviceInfoVCF(data, paramArray){
 document.getElementById(‘devDisplay’).innerText = ‘UDID: ‘+paramArray[0]+‘\n\ndevice name: ‘+paramArray[1]+‘\n\nmodel: ‘+paramArray[2]
+‘\n\nsystem: ‘+paramArray[3]+‘\n\nOS version: ‘+paramArray[4]+‘\n\nLanguage and Locale: ‘+paramArray[5]+‘\n\nTimeZone: ‘+paramArray[6];
}


但是从ios5开始苹果官方不支持获取UniqueIndentifier(UDID) 的方法,原先的方法不管用了。苹果官方又推出了一种新的方法,获取UUID。

新方法的原理为在第一次使用程序的时候用CFUUIDCreate创造一个 UUID,然后将它存到NSUserDefault中,当做以前的UDID来用就行了。不过直接调用CFUUIDCreate得到的还不是一个直接的NSString,需要经过一些步骤才能转换成我们熟悉UDID形式。

具体的实现方法为:

 CFUUIDRef deviceId = CFUUIDCreate (NULL);
   
    CFStringRef deviceIdStringRef = CFUUIDCreateString(NULL,deviceId);
   
    CFRelease(deviceId);

    NSString *deviceIdString = (NSString *)deviceIdStringRef;
   
    NSUserDefaults *standardUserDefault = [NSUserDefaults standardUserDefaults];
   
    [standardUserDefault setValue:deviceIdString forKey:@"deviceId"];
   
    [deviceIdString release];
   
    [standardUserDefault synchronize];


关于CFUUIDCreate,在苹果文档上有说明:

CFUUIDCreate
Creates a Universally Unique Identifier (UUID) object.
CFUUIDRef CFUUIDCreate (
   CFAllocatorRef alloc
);

但是这个方法有一个漏洞就是,在每次该应用重装以后,新的UUID都会改变。而且据网上资料说,UUID不能保证每次在系统升级后还能用。

而开发者获取UDID或者UUID的原因不外乎都是为了能够获得匹配设备的唯一标示。因此网上的最为可行的方法便是获取用户的mac地址,并且

对其进行加密。

具体的解决方法可以参照(下载地址):

https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5

除此之外,你还能获得其他的一些手机信息,如mac地址,序列号,IMEI等信息,具体见下篇博文。
原创粉丝点击