Ios obtains the mac address

Source: Internet
Author: User

 

 

First of all, the following two methods can obtain the mac address of the mobile phone, but there is a limit that can be obtained only on iOS. After iOS7, Apple performed technical processing on sysctl and ioctl. the MAC address returns 02: 00: 00: 00: 00: 00. The "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 shocould migrate toidentifiers such as-[UIDevice identifierForVendor]. this change affects all apps running on iOS 7 ".

Therefore, the unique Mac address of the device cannot be obtained after iOS7 and can only be replaced by another one.

The following two methods are described:

You need to import several header files.

 

[Html]View plaincopyprint?
  1. # Include
  2. # Include
  3. # Include
    Method 1:

     

     

    [Html]View plaincopyprint?
    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 (cocould 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. }
      From http://blog.csdn.net/showhilllee

       

      Method 2:

       

       

      [Html]View plaincopyprint?
      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: % 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. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.