目标C / CAN。当我尝试在IOS设备上获取Mac地址时,我得到错误的值

时间:2015-02-15 14:58:20

标签: ios objective-c iphone c mac-address

我尝试使用C代码在任何IOS设备上获取mac地址。 我在iPhone 6 plus上尝试它,但它似乎不起作用。

我的输出如下:

2015-02-15 15:37:37.947 MyDemo [438:223963] Mac地址:02:00:00:00:00:00

有人可以帮我吗? 感谢。

这是GetMacAdress.m 原始源代码由iOSDeveloperTips.com提供John,

#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

...

- (NSString *)getMacAddress
{
  int                 mgmtInfoBase[6];
  char                *msgBuffer = NULL;
  size_t              length;
  unsigned char       macAddress[6];
  struct if_msghdr    *interfaceMsgStruct;
  struct sockaddr_dl  *socketStruct;
  NSString            *errorFlag = NULL;

  // Setup the management Information Base (mib)
  mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
  mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
  mgmtInfoBase[2] = 0;              
  mgmtInfoBase[3] = AF_LINK;        // Request link layer information
  mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces

  // With all configured interfaces requested, get handle index
  if ((mgmtInfoBase[5] = if_nametoindex("en0")) == 0) 
    errorFlag = @"if_nametoindex failure";
  else
  {
    // Get the size of the data available (store in len)
    if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0) 
      errorFlag = @"sysctl mgmtInfoBase failure";
    else
    {
      // Alloc memory based on above call
      if ((msgBuffer = malloc(length)) == NULL)
        errorFlag = @"buffer allocation failure";
      else
      {
        // Get system information, store in buffer
        if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
          errorFlag = @"sysctl msgBuffer failure";
      }
    }
  }

  // Befor going any further...
  if (errorFlag != NULL)
  {
    NSLog(@"Error: %@", errorFlag);
    return errorFlag;
  }

  // Map msgbuffer to interface message structure
  interfaceMsgStruct = (struct if_msghdr *) msgBuffer;

  // Map to link-level socket structure
  socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

  // Copy link layer address data in socket structure to an array
  memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, 6);

  // Read from char array into a string object, into traditional Mac address format
  NSString *macAddressString = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
                                macAddress[0], macAddress[1], macAddress[2], 
                                macAddress[3], macAddress[4], macAddress[5]];
  NSLog(@"Mac Address: %@", macAddressString);

  // Release the buffer memory
  free(msgBuffer);

  return macAddressString;
}

2 个答案:

答案 0 :(得分:2)

无法再获取苹果设备的MAC地址。添加此项是为了防止应用程序将MAC地址用作跟踪的唯一标识符。无论你找到它的路线如何,它总是在02:00:00:00:00:00出现。

您必须使用的供应商和营销ID不是MAC地址,而是其他内容。

我已经编写了一个网络扫描仪,据我所知,除非你可以在相同的LAN网段上与外部设备通信,并且能够看到设备在网络上的可见性,否则无法解决这个问题。发给你。

答案 1 :(得分:2)

这是一些非常讨厌的低级代码。

我个人认为编写表格代码的人:

if ((a=b)==c) 

......应该被枪杀。我们不在这里编写汇编程序 - 不要编写看起来像拼写错误的代码来保存1个语句。 (那个咆哮是针对原作者,而不是你。)

我对低级系统功能不够熟悉,能够在没有大量挖掘的情况下分辨出错误。

但是,为什么您的代码无法正常工作,为什么需要MAC地址呢?它必须是实际的网络MAC地址吗?

Apple不再允许您获取该信息,因为它允许您唯一地跟踪用户的设备。如果您确实找到了办法,您的应用将被拒绝。 (不是Apple的错;行业中存在很大的问题,所有设备供应商都不得不阻止提供这些信息。)

@ mad_mask建议使用identifierForVendor可能是最好的解决方案。这为您的公司提供了该设备唯一的ID。