因为项目需要, 所以需要使用到一个唯一识别一个硬件的方法。
当然我们知道,目前uuid已经被苹果给禁用。
即如下方法取uuid串。会取得一个40位的串。
UIDevice *myDevice = [UIDevicecurrentDevice];
NSString *deviceUDID = [myDevice uniqueIdentifier];
但因为uuid已被禁用,所以我们只得想其它办法,结果在网上搜到下面这个方法,经验证, 确实可以生成一个唯一的串,但是是32位的,不知是什么东西。但看起来似乎确实是唯一的。 有知道的, 还请留言分享一下,谢谢!
- -(NSString*)createUUID
- {
- //Createuniversallyuniqueidentifier(object)
- CFUUIDRefuuidObject=CFUUIDCreate(kCFAllocatorDefault);
- //GetthestringrepresentationofCFUUIDobject.
- NSString*uuidStr=(NSString*)CFUUIDCreateString(kCFAllocatorDefault,uuidObject);
- //Ifneeded,hereishowtogetarepresentationinbytes,returnedasastructure
- //typedefstruct{
- //UInt8byte0;
- //UInt8byte1;
- //...
- //UInt8byte15;
- //}CFUUIDBytes;
- CFUUIDBytesbytes=CFUUIDGetUUIDBytes(uuidObject);
- CFRelease(uuidObject);
- returnuuidStr;
- }
下面是我iphone4的结果
createUUID=A450EA27-xxxx-xxxx-xxxx-02B752DB82FA,再次运行。发现它是F783E91C-A0DD-4B06-xxxx-CA7967B049EE, 所以可以认为它确实不是唯一的。
deviceUDID=c96f6d0f9321df1cce375bcf7d6e50fe(末尾去掉8位,呵呵,隐私)
1.通过调用CFFUUIDCreate函数来生成机器唯一标识符,但每次调用以下函数返回的字符串都不一样,所以第一次调用后需把该字符串存储起来。这是官方API的建议方法
- (NSString*) uniqueString
{
CFUUIDRefunique =CFUUIDCreate(kCFAllocatorDefault);
NSString*result = [(NSString*)CFUUIDCreateString(kCFAllocatorDefault, unique)autorelease];
CFRelease(unique);
returnresult;
}
2.由于机器的mac地址都不一样,所以可通过获取mac地址后再进行md5加密处理后返回的字符串作为唯一标识码, Heqin:原理就是通过Mac地址,然后再用md5值处理一下,最终成为唯一标识码。个人认为,因为Mac地址本身的唯一性,其实也可以做为唯一识别的身份。(除非有识备没有上网模块,但好像没有这样的设备), 注意:原贴中没有包含下面这几个 include会导致编译失败,需要加上。
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
- (NSString*) macaddress{
int mib[6];
size_t len;
char *buf;
unsignedchar *ptr;
structif_msghdr *ifm;
structsockaddr_dl *sdl;
mib[0] =CTL_NET;
mib[1] =AF_ROUTE;
mib[2] =0;
mib[3] =AF_LINK;
mib[4] =NET_RT_IFLIST;
if((mib[5] =if_nametoindex("en0")) ==0) {
printf("Error: if_nametoindex error\n");
returnNULL;
}
if(sysctl(mib,6,NULL, &len,NULL,0) <0) {
printf("Error: sysctl, take 1\n");
returnNULL;
}
if((buf =malloc(len)) ==NULL) {
printf("Could not allocate memory. error!\n");
returnNULL;
}
if(sysctl(mib,6, buf, &len,NULL,0) <0) {
printf("Error: sysctl, take 2");
returnNULL;
}
ifm = (structif_msghdr*)buf;
sdl = (structsockaddr_dl*)(ifm +1);
ptr = (unsignedchar*)LLADDR(sdl);
NSString*outstring = [NSStringstringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
returnoutstring;
}
- (NSString*) uniqueGlobalDeviceIdentifier{
NSString*macaddress = [[UIDevicecurrentDevice]macaddress];
NSString*uniqueIdentifier = [macaddressstringFromMD5];
returnuniqueIdentifier;
}
NSString类别 Heqin:下面这个方法需要做如下引用,原贴没有加,会编译通不过:
#import <CommonCrypto/CommonDigest.h>
@implementationNSString(MD5Addition)
- (NSString*) stringFromMD5{
if(self==nil|| [selflength] ==0)
returnnil;
constchar*value = [selfUTF8String];
unsignedcharoutputBuffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(value,strlen(value), outputBuffer);
NSMutableString*outputString = [[NSMutableStringalloc]initWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(NSIntegercount =0; count <CC_MD5_DIGEST_LENGTH; count++){
[outputStringappendFormat:@"%02x",outputBuffer[count]];
}
return[outputStringautorelease];
转http://blog.163.com/ray_jun/blog/static/1670536422011102744836300/
}