[转]:获取UIDevice的uniqueIdentifier替代方法

1.通过调用CFFUUIDCreate函数来生成机器唯一标识符,但每次调用以下函数返回的字符串都不一样,所以第一次调用后需把该字符串存储起来。这是官方API的建议方法

- (NSString *) uniqueString

{

CFUUIDRef unique = CFUUIDCreate(kCFAllocatorDefault);

NSString *result = [(NSString *)CFUUIDCreateString(kCFAllocatorDefault, unique) autorelease];

CFRelease(unique);

return result;

}

 

 

2.由于机器的mac地址都不一样,所以可通过获取mac地址后再进行md5加密处理后返回的字符串作为唯一标识码

 

  1 - (NSString *) macaddress{
  2 
  3     
  4 
  5     int                 mib[6];
  6 
  7     size_t              len;
  8 
  9     char                *buf;
 10 
 11     unsigned char       *ptr;
 12 
 13     struct if_msghdr    *ifm;
 14 
 15     struct sockaddr_dl  *sdl;
 16 
 17     
 18 
 19     mib[0] = CTL_NET;
 20 
 21     mib[1] = AF_ROUTE;
 22 
 23     mib[2] = 0;
 24 
 25     mib[3] = AF_LINK;
 26 
 27     mib[4] = NET_RT_IFLIST;
 28 
 29     
 30 
 31     if ((mib[5] = if_nametoindex("en0")) == 0) {
 32 
 33         printf("Error: if_nametoindex error\n");
 34 
 35         return NULL;
 36 
 37     }
 38 
 39     
 40 
 41     if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
 42 
 43         printf("Error: sysctl, take 1\n");
 44 
 45         return NULL;
 46 
 47     }
 48 
 49     
 50 
 51     if ((buf = malloc(len)) == NULL) {
 52 
 53         printf("Could not allocate memory. error!\n");
 54 
 55         return NULL;
 56 
 57     }
 58 
 59     
 60 
 61     if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
 62 
 63         printf("Error: sysctl, take 2");
 64 
 65         return NULL;
 66 
 67     }
 68 
 69     
 70 
 71     ifm = (struct if_msghdr *)buf;
 72 
 73     sdl = (struct sockaddr_dl *)(ifm + 1);
 74 
 75     ptr = (unsigned char *)LLADDR(sdl);
 76 
 77     NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
 78 
 79                            *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
 80 
 81     free(buf);
 82 
 83     
 84 
 85     return outstring;
 86 
 87 }
 88 
 89  
 90 
 91  
 92 
 93 - (NSString *) uniqueGlobalDeviceIdentifier{
 94 
 95     NSString *macaddress = [[UIDevice currentDevice] macaddress];
 96 
 97     NSString *uniqueIdentifier = [macaddress stringFromMD5];
 98 
 99     
100 
101     return uniqueIdentifier;
102 
103 }
104 
105  
106 
107 NSString类别
108 
109  
110 
111 @implementation NSString(MD5Addition)
112 
113  
114 
115 - (NSString *) stringFromMD5{
116 
117     
118 
119     if(self == nil || [self length] == 0)
120 
121         return nil;
122 
123     
124 
125     const char *value = [self UTF8String];
126 
127     
128 
129     unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
130 
131     CC_MD5(value, strlen(value), outputBuffer);
132 
133     
134 
135     NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
136 
137     for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
138 
139         [outputString appendFormat:@"%02x",outputBuffer[count]];
140 
141     }
142 
143     
144 
145     return [outputString autorelease];
146 
147 }

 

原文地址:http://blog.163.com/ray_jun/blog/static/1670536422011102744836300/

posted @ 2013-07-31 09:15  windworship  阅读(180)  评论(0编辑  收藏  举报