UIDevice+IdentifierAddition.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // UIDevice(Identifier).m
  3. // UIDeviceAddition
  4. //
  5. // Created by Georg Kitz on 20.08.11.
  6. // Copyright 2011 Aurora Apps. All rights reserved.
  7. //
  8. #import "UIDevice+IdentifierAddition.h"
  9. #import "NSString+MD5Addition.h"
  10. #include <sys/socket.h> // Per msqr
  11. #include <sys/sysctl.h>
  12. #include <net/if.h>
  13. #include <net/if_dl.h>
  14. @interface UIDevice(Private)
  15. - (NSString *) macaddress;
  16. @end
  17. @implementation UIDevice (IdentifierAddition)
  18. ////////////////////////////////////////////////////////////////////////////////
  19. #pragma mark -
  20. #pragma mark Private Methods
  21. // Return the local MAC addy
  22. // Courtesy of FreeBSD hackers email list
  23. // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
  24. - (NSString *) macaddress{
  25. int mib[6];
  26. size_t len;
  27. char *buf;
  28. unsigned char *ptr;
  29. struct if_msghdr *ifm;
  30. struct sockaddr_dl *sdl;
  31. mib[0] = CTL_NET;
  32. mib[1] = AF_ROUTE;
  33. mib[2] = 0;
  34. mib[3] = AF_LINK;
  35. mib[4] = NET_RT_IFLIST;
  36. if ((mib[5] = if_nametoindex("en0")) == 0) {
  37. printf("Error: if_nametoindex error\n");
  38. return NULL;
  39. }
  40. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  41. printf("Error: sysctl, take 1\n");
  42. return NULL;
  43. }
  44. if ((buf = malloc(len)) == NULL) {
  45. printf("Could not allocate memory. error!\n");
  46. return NULL;
  47. }
  48. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  49. printf("Error: sysctl, take 2");
  50. return NULL;
  51. }
  52. ifm = (struct if_msghdr *)buf;
  53. sdl = (struct sockaddr_dl *)(ifm + 1);
  54. ptr = (unsigned char *)LLADDR(sdl);
  55. NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X",
  56. *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  57. free(buf);
  58. return outstring;
  59. }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. #pragma mark -
  62. #pragma mark Public Methods
  63. - (NSString *) uniqueDeviceIdentifier{
  64. NSString *macaddress = [[UIDevice currentDevice] macaddress];
  65. NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
  66. NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
  67. NSString *uniqueIdentifier = [stringToHash stringFromMD5];
  68. return uniqueIdentifier;
  69. }
  70. - (NSString *) uniqueGlobalDeviceIdentifier{
  71. NSString *macaddress = [[UIDevice currentDevice] macaddress];
  72. NSString *uniqueIdentifier = [macaddress stringFromMD5];
  73. return uniqueIdentifier;
  74. }
  75. @end