你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios獲取mac地址

ios獲取mac地址

編輯:IOS開發綜合

 

 

首先說明下,下面兩種方法均可以獲得手機的mac地址,但是有個限制,是在iOS一下才可以獲得。iOS7以後蘋果對於sysctl和ioctl進行了技術處理,MAC地址返回的都是02:00:00:00:00:00。官方文檔上這樣寫的“Twolow-level networking APIs that used to return a MAC address now return thefixed value 02:00:00:00:00:00. The APIs in question are sysctl(NET_RT_IFLIST) and ioctl(SIOCGIFCONF). Developers using the value of the MAC address should migrate toidentifiers such as -[UIDevice identifierForVendor].This change affects all apps running on iOS 7”。

所以在iOS7以後想要獲取設備的唯一標示Mac地址已經不行了,只能用其他的代替。

下面說下兩種方式:

都需要導入幾個頭文件

 

[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
  1. #include
  2. #include
  3. #include
    方法1:

     

     

    [html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
    1. // Return the local MAC addy
    2. // Courtesy of FreeBSD hackers email list
    3. // Accidentally munged during previous update. Fixed thanks to mlamb.
    4. - (NSString *) macaddress
    5. {
    6.  
    7. int mib[6];
    8. size_t len;
    9. char *buf;
    10. unsigned char *ptr;
    11. struct if_msghdr *ifm;
    12. struct sockaddr_dl *sdl;
    13.  
    14. mib[0] = CTL_NET;
    15. mib[1] = AF_ROUTE;
    16. mib[2] = 0;
    17. mib[3] = AF_LINK;
    18. mib[4] = NET_RT_IFLIST;
    19.  
    20. if ((mib[5] = if_nametoindex(en0)) == 0) {
    21. printf(Error: if_nametoindex error/n);
    22. return NULL;
    23. }
    24.  
    25. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
    26. printf(Error: sysctl, take 1/n);
    27. return NULL;
    28. }
    29.  
    30. if ((buf = malloc(len)) == NULL) {
    31. printf(Could not allocate memory. error!/n);
    32. return NULL;
    33. }
    34.  
    35. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
    36. printf(Error: sysctl, take 2);
    37. return NULL;
    38. }
    39.  
    40. ifm = (struct if_msghdr *)buf;
    41. sdl = (struct sockaddr_dl *)(ifm + 1);
    42. ptr = (unsigned char *)LLADDR(sdl);
    43. NSString *outstring = [NSString stringWithFormat:@%02x:%02x:%02x:%02x:%02x:%02x, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    44.  
    45. // NSString *outstring = [NSString stringWithFormat:@%02x%02x%02x%02x%02x%02x, *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    46.  
    47. NSLog(@outString:%@, outstring);
    48.  
    49. free(buf);
    50.  
    51. return [outstring uppercaseString];
    52. }
      源自http://blog.csdn.net/showhilllee

       

      方法2:

       

       

      [html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
      1. - (NSString *)getMacAddress
      2. {
      3. int mgmtInfoBase[6];
      4. char *msgBuffer = NULL;
      5. size_t length;
      6. unsigned char macAddress[6];
      7. struct if_msghdr *interfaceMsgStruct;
      8. struct sockaddr_dl *socketStruct;
      9. NSString *errorFlag = NULL;
      10.  
      11. // Setup the management Information Base (mib)
      12. mgmtInfoBase[0] = CTL_NET; // Request network subsystem
      13. mgmtInfoBase[1] = AF_ROUTE; // Routing table info
      14. mgmtInfoBase[2] = 0;
      15. mgmtInfoBase[3] = AF_LINK; // Request link layer information
      16. mgmtInfoBase[4] = NET_RT_IFLIST; // Request all configured interfaces
      17.  
      18. // With all configured interfaces requested, get handle index
      19. if ((mgmtInfoBase[5] = if_nametoindex(en0)) == 0)
      20. errorFlag = @if_nametoindex failure;
      21. else
      22. {
      23. // Get the size of the data available (store in len)
      24. if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
      25. errorFlag = @sysctl mgmtInfoBase failure;
      26. else
      27. {
      28. // Alloc memory based on above call
      29. if ((msgBuffer = malloc(length)) == NULL)
      30. errorFlag = @buffer allocation failure;
      31. else
      32. {
      33. // Get system information, store in buffer
      34. if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
      35. errorFlag = @sysctl msgBuffer failure;
      36. }
      37. }
      38. }
      39.  
      40. // Befor going any further...
      41. if (errorFlag != NULL)
      42. {
      43. NSLog(@Error: %@, errorFlag);
      44. return errorFlag;
      45. }
      46.  
      47. // Map msgbuffer to interface message structure
      48. interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
      49.  
      50. // Map to link-level socket structure
      51. socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
      52.  
      53. // Copy link layer address data in socket structure to an array
      54. memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);
      55.  
      56. // Read from char array into a string object, into traditional Mac address format
      57. NSString *macAddressString = [NSString stringWithFormat:@%02x:%02x:%02x:%02x:%02x:%02x,
      58. macAddress[0], macAddress[1], macAddress[2],
      59. macAddress[3], macAddress[4], macAddress[5]];
      60. NSLog(@Mac Address: %@, macAddressString);
      61.  
      62. // Release the buffer memory
      63. free(msgBuffer);
      64.  
      65. return macAddressString;
      66. }
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved