ios - Objective-C/C。当我尝试在 IOS 设备上获取 Mac 地址时,我得到了错误的值

标签 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;
}

最佳答案

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

我个人认为任何编写以下形式的代码的人:

if ((a=b)==c) 

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

我对低级系统功能不够熟悉,无法在不进行大量挖掘的情况下判断出问题所在。

但是,从为什么您的代码无法正常工作的问题上退一步,为什么您需要 MAC 地址?是否必须是实际的网络 MAC 地址?

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

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

关于ios - Objective-C/C。当我尝试在 IOS 设备上获取 Mac 地址时,我得到了错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28527355/

相关文章:

ios - 你可以在一个 Xcode 项目中拥有多个 Root View Controller 吗?

ios - 拒绝许可后的 Facebook 登录

ios - 如何在 iOS 上使用嵌入式 HTTP 服务器实现 Video Seek 支持?

ios - 无法通过 UIView 通过 swift 创建 pdf?

iphone - NSString 为空

ios - UIImage drawInRect

iphone - 由于使用保留属性而导致内存泄漏

iOS7 viewController 和 MPMoviePlayerViewController 旋转

ios - iOS 是否支持 SVG Tiny?

iphone - 在横向模式下启动(启动图像)?