[英]how can i get programmaticaly in Objective-C the name of local domain Knowing the ip adress?
[英]Objective C / C. I get wrong Value when i try to get Mac Adress on IOS device
我正在尝试使用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提供
#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;
}
无法再获取Apple设备的MAC地址。 这样做是为了防止应用程序将MAC地址用作跟踪的唯一标识符。 无论您采用何种查找路线,它始终会显示为02:00:00:00:00:00。
您必须使用的供应商和市场ID不是MAC地址,而是其他东西。
我已经写了一个网络扫描仪,据我所知,除非您可以与同一LAN网段上的外部设备进行对话(该设备可以看到网络上的设备并可以将其发送到),否则就无法解决此问题。您。
那是一些非常讨厌的底层代码。
我个人认为任何编写以下形式的代码的人:
if ((a=b)==c)
...应该被枪杀。 我们不是在这里编写汇编程序-不要编写看起来像错字的代码来保存1条语句。 (那个咆哮是针对原始作者的,而不是针对您的。)
我对底层系统功能还不够熟悉,因此无需过多挖掘就能知道出了什么问题。
但是,从为什么您的代码不起作用的原因退后一步,为什么需要MAC地址? 是否必须是实际的网络MAC地址?
Apple不再允许您获取该信息,因为它允许您唯一地跟踪用户的设备。 如果您确实找到了解决方法,则您的应用将被拒绝。 (这并不是苹果公司的错;这是一个很大的臭味,所有设备供应商都必须阻止提供此信息。)
@mad_mask关于使用identifierForVendor的建议可能是最好的解决方案。 这将为您的公司提供唯一的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.