【问题标题】:Objective C / C. I get wrong Value when i try to get Mac Adress on IOS device目标 C / C。当我尝试在 IOS 设备上获取 Mac 地址时,我得到了错误的值
【发布时间】:2015-02-15 14:58:20
【问题描述】:

我正在尝试使用 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;
}

【问题讨论】:

  • 我使用 UIDevice 的 identifierForVendor 方法得到了 mac 地址。 //从设备获取Mac地址。 UIDevice *device = [[UIDevice alloc]init]; NSString *macAdress = [NSString stringWithFormat:@"%@", [device identifierForVendor]]; NSLog(@"%@",macAdress);但是让它与 C 代码一起工作会很好。 :)
  • 这不是 iOS7/8 上的 MAC 地址,而是苹果生成的唯一标识符。

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


【解决方案1】:

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

我个人认为,任何编写表单代码的人:

if ((a=b)==c) 

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

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

但是,从您的代码无法运行的原因退一步说,您为什么需要 MAC 地址?它必须是实际的网络 MAC 地址吗?

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

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

【讨论】:

  • 我使用 iBeacon。我想知道是否已经在我的信标范围内找到了设备。在我的情况下,identifierForVendor 会起作用吗?该密钥仅对我的公司是唯一的吗?如果我了解同一设备,我公司的每个应用程序都会获得相同的 ID,但如果另一家公司制作了一个应用程序,他们将获得另一个?请告诉我。非常感谢您的指导。谢谢!
  • 我很确定 identifierForVendor 对于公司的设备来说是独一无二的。因此,如果我有 3 台 iOS 设备,每台设备都会为您的应用程序报告不同的 UUID,而为另一家公司的应用程序报告一组不同的 ID。为什么不去阅读有关该方法的文档?这是此类问题答案的权威来源。
【解决方案2】:

无法再获取苹果设备的 MAC 地址。添加此功能是为了防止应用程序使用 MAC 地址作为跟踪的唯一标识符。它总是以 02:00:00:00:00:00 的形式出现,无论您通过何种途径找到它。

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

我已经编写了一个网络扫描仪,据我所知,没有办法解决这个问题,除非您可以与同一 LAN 网段上的外部设备通信,该设备在网络上具有可见性并且可以发给你。

【讨论】:

  • 只是好奇:您的扫描仪是开源的吗?
  • 抱歉它不是开源的。
猜你喜欢
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多