ios 如何以编程方式获取iOS设备MAC地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14827365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get iOS device MAC address programmatically
提问by iPhoneDeveloper
How do I get an iOS device's MAC code programmatically in my app?
如何在我的应用程序中以编程方式获取 iOS 设备的 MAC 代码?
回答by J?hn Narr?w
Now iOS 7 devices – are always returning a MAC address of 02:00:00:00:00:00.
现在 iOS 7 设备 – 总是返回 02:00:00:00:00:00 的 MAC 地址。
So better use [UIDevice identifierForVendor]
所以更好地使用 [UIDevice identifierForVendor]
so better to call this methodto get app specific unique key
所以最好调用这个方法来获取应用程序特定的唯一键
Category will more suitable
类别会更合适
#import "UIDevice+Identifier.h"
- (NSString *) identifierForVendor1
{
if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) {
return [[[UIDevice currentDevice] identifierForVendor] UUIDString];
}
return @"";
}
Now call above method to get unique address
现在调用上面的方法来获取唯一的地址
NSString *like_UDID=[NSString stringWithFormat:@"%@",
[[UIDevice currentDevice] identifierForVendor1]];
NSLog(@"%@",like_UDID);
回答by Guy Kahlon
You can get the MAC Adress using the following function:
您可以使用以下函数获取 MAC 地址:
+(NSString *)getMacAddress
{
int mgmtInfoBase[6];
char *msgBuffer = NULL;
NSString *errorFlag = NULL;
size_t length;
// 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";
// Get the size of the data available (store in len)
else if (sysctl(mgmtInfoBase, 6, NULL, &length, NULL, 0) < 0)
errorFlag = @"sysctl mgmtInfoBase failure";
// Alloc memory based on above call
else if ((msgBuffer = malloc(length)) == NULL)
errorFlag = @"buffer allocation failure";
// Get system information, store in buffer
else if (sysctl(mgmtInfoBase, 6, msgBuffer, &length, NULL, 0) < 0)
{
free(msgBuffer);
errorFlag = @"sysctl msgBuffer failure";
}
else
{
// Map msgbuffer to interface message structure
struct if_msghdr *interfaceMsgStruct = (struct if_msghdr *) msgBuffer;
// Map to link-level socket structure
struct sockaddr_dl *socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);
// Copy link layer address data in socket structure to an array
unsigned char macAddress[6];
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]];
if(IsDEBUG) NSLog(@"Mac Address: %@", macAddressString);
// Release the buffer memory
free(msgBuffer);
return macAddressString;
}
// Error...
if(IsDEBUG) NSLog(@"Error: %@", errorFlag);
return errorFlag;
}
But as he said #Randomclik, mac address is unavailable from ios 7 and up.
但是正如他所说的#Randomclik,ios 7 及更高版本无法使用mac 地址。
form apple:
形成苹果:
In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02:00:00:00:00:00. If you need to identify the device, use the identifierForVendor property of UIDevice instead. (Apps that need an identifier for their own advertising purposes should consider using the advertisingIdentifier property of ASIdentifierManager instead.)
在 iOS 7 及更高版本中,如果您询问 iOS 设备的 MAC 地址,系统会返回值 02:00:00:00:00:00。如果您需要识别设备,请改用 UIDevice 的 identifierForVendor 属性。(需要标识符用于自己的广告目的的应用程序应考虑改用 ASIdentifierManager 的 AdvertisingIdentifier 属性。)
链接:https: //developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW1
Conversation about MAC Address:
关于 MAC 地址的对话:
回答by Randomclik
It seems that in iOS 7 and later get MAC addres will not work. Look at What's new on iOS document from Apple. (Deprecated APIs section.)
似乎在 iOS 7 及更高版本中获取 MAC 地址不起作用。查看 Apple 提供的 iOS 新功能文档。(已弃用的 API 部分。)
回答by Karsten
everybody is good advised to use the offical ios 7 way and use [UIDevice identifierForVendor]
建议大家使用官方的ios 7方式并使用[UIDevice identifierForVendor]
And also think about the migration from some older assumptions.
还要考虑从一些较旧的假设迁移。
回答by sambavadekar
You can get it by combining the following two answers i guess.
我猜你可以通过结合以下两个答案来获得它。
First, find ip of the device by using the following:
首先,使用以下命令查找设备的 ip:
https://stackoverflow.com/a/30754194/1089206
https://stackoverflow.com/a/30754194/1089206
Second, find the MAC of that address, by using the following:
其次,使用以下命令找到该地址的 MAC:
https://stackoverflow.com/a/31246085/1089206
https://stackoverflow.com/a/31246085/1089206
I'll be trying it out now and will let you know how it goes.
我现在会尝试一下,让你知道它是怎么回事。