iOS5中的UUID


在ios5中,UDID已不再被推荐使用,在将来的版本中,这个功能可能会消失。所以我们得探寻它的取代方法,能唯一标识设备的东西。往往硬件上有唯一标识,所以我们可以用硬件上的信息来取代UDID, 硬件上的MAC地址就能达到这样的目的。

下面的函数就可以返回XX:XX:XX:XX:XX:XX类型的字符串(12个16进制数)

  1. #include <sys/socket.h>   
  2. #include <sys/sysctl.h>   
  3. #include <net/if.h>   
  4. #include <net/if_dl.h>   
  5.    
  6. ...  
  7.    
  8. - (NSString *)getMacAddress  
  9. {  
  10.   int                 mgmtInfoBase[6];  
  11.   char                *msgBuffer = NULL;  
  12.   size_t              length;  
  13.   unsigned char       macAddress[6];  
  14.   struct if_msghdr    *interfaceMsgStruct;  
  15.   struct sockaddr_dl  *socketStruct;  
  16.   NSString            *errorFlag = NULL;  
  17.    
  18.   // Setup the management Information Base (mib)   
  19.   mgmtInfoBase[0] = CTL_NET;        // Request network subsystem   
  20.   mgmtInfoBase[1] = AF_ROUTE;       // Routing table info   
  21.   mgmtInfoBase[2] = 0;                
  22.   mgmtInfoBase[3] = AF_LINK;        // Request link layer information   
  23.   mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces   
  24.    
  25.   // With all configured interfaces requested, get handle index   
  26.   if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0)   
  27.     errorFlag = @"if_nametoindex failure";  
  28.   else  
  29.   {  
  30.     // Get the size of the data available (store in len)   
  31.     if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)   
  32.       errorFlag = @"sysctl mgmtInfoBase failure";  
  33.     else  
  34.     {  
  35.       // Alloc memory based on above call   
  36.       if ((msgBuffer = malloc(length)) == NULL)  
  37.         errorFlag = @"buffer allocation failure";  
  38.       else  
  39.       {  
  40.         // Get system information, store in buffer   
  41.         if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)  
  42.           errorFlag = @"sysctl msgBuffer failure";  
  43.       }  
  44.     }  
  45.   }  
  46.    
  47.   // Befor going any further...   
  48.   if (errorFlag != NULL)  
  49.   {  
  50.     NSLog(@"Error: %@", errorFlag);  
  51.     return errorFlag;  
  52.   }  
  53.    
  54.   // Map msgbuffer to interface message structure   
  55.   interfaceMsgStruct = (struct if_msghdr *) msgBuffer;  
  56.    
  57.   // Map to link-level socket structure   
  58.   socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);  
  59.    
  60.   // Copy link layer address data in socket structure to an array   
  61.   memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);  
  62.    
  63.   // Read from char array into a string object, into traditional Mac address format   
  64.   NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",   
  65.                                 macAddress[0], macAddress[1], macAddress[2],   
  66.                                 macAddress[3], macAddress[4], macAddress[5]];  
  67.   NSLog(@"Mac Address: %@", macAddressString);  
  68.    
  69.   // Release the buffer memory   
  70.   free(msgBuffer);  
  71.    
  72.   return macAddressString;  
  73. }  

系统SKD也提供了一种方法得到标识字符串即UDID,如下:

  1. [[UIDevice currentDevice] uniqueIdentifier]  

但打开UIDevice.h中你会发现这样的定义与注释

  1. @property(nonatomic,readonly,retain) NSString    *uniqueIdentifier  __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_5_0);  // a string unique to each device based on various hardware info.  
说明苹果将逐渐淘汰这个属性。


UUID在iOS4中也可以用,但不能保证在以后的系统升级后(iOS6 , 7)还能用。ios5中我还没有测试过,因为现在手里没设备了(测了的朋友给我说说结果)。

在此也随便把得到UUID的方法以写出来:

  1. -(NSString*) uuid  
  2. {  
  3. CFUUIDRef puuid = CFUUIDCreate( nil );  
  4. CFStringRef uuidString = CFUUIDCreateString( nil, puuid );  
  5. NSString * result = (NSString *)CFStringCreateCopy( NULL, uuidString);  
  6. CFRelease(puuid);  
  7. CFRelease(uuidString);  
  8. return [result autorelease];  
  9. }  

在这儿有两个概念UUID与UDID.

UUID是Universally Unique Identifier 通用唯一标识码

UDID是Unique Device Identifier 设备唯一标识码

UDID只是UUID的一个了集而已。


目前我知道的就是用这些方法来唯一标识设备,如果你有更好的方法,也希望你能与大家分享。

相关内容