gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:30 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com