歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS5中的UUID

iOS5中的UUID

日期:2017/3/1 11:02:37   编辑:Linux編程

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

系統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的一個了集而已。


目前我知道的就是用這些方法來唯一標識設備,如果你有更好的方法,也希望你能與大家分享。
Copyright © Linux教程網 All Rights Reserved