因为项目需要, 所以需要使用到一个唯一识别一个硬件的方法。
当然我们知道,目前uuid已经被苹果给禁用。
即如下方法取uuid串。会取得一个40位的串。
UIDevice *myDevice = [UIDevicecurrentDevice];
NSString *deviceUDID = [myDevice uniqueIdentifier];
但因为uuid已被禁用,所以我们只得想其它办法,结果在网上搜到下面这个方法,经验证, 确实可以生成一个唯一的串,但是是32位的,不知是什么东西。但看起来似乎确实是唯一的。 有知道的, 还请留言分享一下,谢谢!
- - (NSString *)createUUID
- {
- // Create universally unique identifier (object)
- CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
- // Get the string representation of CFUUID object.
- NSString *uuidStr = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uuidObject);
- // If needed, here is how to get a representation in bytes, returned as a structure
- // typedef struct {
- // UInt8 byte0;
- // UInt8 byte1;
- // ...
- // UInt8 byte15;
- // } CFUUIDBytes;
- CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuidObject);
- CFRelease(uuidObject);
- return uuidStr;
- }
下面是我iphone4的结果
createUUID=A450EA27-xxxx-xxxx-xxxx-02B752DB82FA,再次运行。发现它是F783E91C-A0DD-4B06-xxxx-CA7967B049EE, 所以可以认为它确实不是唯一的。
deviceUDID=c96f6d0f9321df1cce375bcf7d6e50fe(末尾去掉8位,呵呵,隐私)
1.通过调用CFFUUIDCreate函数来生成机器唯一标识符,但每次调用以下函数返回的字符串都不一样,所以第一次调用后需把该字符串存储起来。这是官方API的建议方法
- (NSString *) uniqueString
{
CFUUIDRef unique = CFUUIDCreate(kCFAllocatorDefault);
NSString *result = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, unique) autorelease];
CFRelease(unique);
return result;
}
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;
unsigned char *ptr;
struct if_msghdr *ifm;
struct sockaddr_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");
return NULL;
}
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 1\n");
return NULL;
}
if ((buf = malloc(len)) == NULL) {
printf("Could not allocate memory. error!\n");
return NULL;
}
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
printf("Error: sysctl, take 2");
return NULL;
}
ifm = (struct if_msghdr *)buf;
sdl = (struct sockaddr_dl *)(ifm + 1);
ptr = (unsigned char *)LLADDR(sdl);
NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
*ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
free(buf);
return outstring;
}
- (NSString *) uniqueGlobalDeviceIdentifier{
NSString *macaddress = [[UIDevice currentDevice] macaddress];
NSString *uniqueIdentifier = [macaddress stringFromMD5];
return uniqueIdentifier;
}
NSString类别 Heqin:下面这个方法需要做如下引用,原贴没有加,会编译通不过:
#import <CommonCrypto/CommonDigest.h>
@implementation NSString(MD5Addition)
- (NSString *) stringFromMD5{
if(self == nil || [self length] == 0)
return nil;
const char *value = [self UTF8String];
unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
CC_MD5(value, strlen(value), outputBuffer);
NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
[outputString appendFormat:@"%02x",outputBuffer[count]];
}
return [outputString autorelease];
转http://blog.163.com/ray_jun/blog/static/1670536422011102744836300/
}