如何以编程方式获取iPhone的MAC地址和IP地址?
注意从iOS7开始,您无法再检索设备MAC地址.将返回固定值而不是实际MAC
我偶然发现了一些事情.最初从这里我修改了一下并清理了一些东西.
IPAddress.h
IPAddress.c
并使用它
InitAddresses(); GetIPAddresses(); GetHWAddresses(); int i; NSString *deviceIP = nil; for (i=0; i适配器名称取决于模拟器/设备以及设备上的wifi或单元.
@abc:我建议你发布另一个问题.最好把它埋在一个不同问题的评论中.
2> ArtFeel..:更新:这在iOS 7上不起作用.您应该使用ASIdentifierManager.
MobileDeveloperTips网站上更干净的解决方案:
#include#include #include #include ... - (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; }
这在iOS7的iPhone5上无效.它给了我:02:00:00:00:00:00这是不对的.
@Brenden你不能再使用iOS7上的PublicAPI获取网络MAC地址了.因此,如果您需要唯一ID,则应使用`IdentifierManager`
@SjoerdPerfors在iOS 7下,您确实总会得到相同的地址`02:00:00:00:00:00`作为Apple的隐私预防措施.
3> shipmaster..:无论是否启用了wifi,我都希望返回地址,因此所选择的解决方案对我不起作用.经过一些调整,我在一些论坛上找到了另一个电话.我最终得到了以下内容(原谅我生锈的C):
#include#include #include #include #include #include char* getMacAddress(char* macAddress, char* ifName) { int success; struct ifaddrs * addrs; struct ifaddrs * cursor; const struct sockaddr_dl * dlAddr; const unsigned char* base; int i; success = getifaddrs(&addrs) == 0; if (success) { cursor = addrs; while (cursor != 0) { if ( (cursor->ifa_addr->sa_family == AF_LINK) && (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) && strcmp(ifName, cursor->ifa_name)==0 ) { dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr; base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen]; strcpy(macAddress, ""); for (i = 0; i < dlAddr->sdl_alen; i++) { if (i != 0) { strcat(macAddress, ":"); } char partialAddr[3]; sprintf(partialAddr, "%02X", base[i]); strcat(macAddress, partialAddr); } } cursor = cursor->ifa_next; } freeifaddrs(addrs); } return macAddress; } 然后我会称它为en0,如下所示:
char* macAddressString= (char*)malloc(18); NSString* macAddress= [[NSString alloc] initWithCString:getMacAddress(macAddressString, "en0") encoding:NSMacOSRomanStringEncoding]; free(macAddressString);
我不得不添加#define IFT_ETHER 0x6
要清楚,代码本身不会泄漏,评论者在最后谈论调用片段,在那里我分配两个字符串而不释放它们.
4> Hejazi..:从iOS 7开始,
02:00:00:00:00:00
当您在任何设备上请求MAC地址时,系统始终返回该值.在iOS 7及更高版本中,如果您要求提供iOS设备的MAC地址,系统将返回值02:00:00:00:00:00.如果需要识别设备,请改用UIDevice的identifierForVendor属性.(需要为自己的广告目的使用标识符的应用应考虑使用ASIdentifierManager的advertisingIdentifier属性.)"
参考:释放说明
5> Kenial..:关于这一点有不同的解决方案,但我找不到整件事.所以我自己做了一个解决方案:
nicinfo
如何使用 :
NICInfoSummary* summary = [[[NICInfoSummary alloc] init] autorelease]; // en0 is for WiFi NICInfo* wifi_info = [summary findNICInfo:@"en0"]; // you can get mac address in 'XX-XX-XX-XX-XX-XX' form NSString* mac_address = [wifi_info getMacAddressWithSeparator:@"-"]; // ip can be multiple if(wifi_info.nicIPInfos.count > 0) { NICIPInfo* ip_info = [wifi_info.nicIPInfos objectAtIndex:0]; NSString* ip = ip_info.ip; NSString* netmask = ip_info.netmask; NSString* broadcast_ip = ip_info.broadcastIP; } else { NSLog(@"WiFi not connected!"); }
6> Grantland Ch..:这看起来非常干净:UIDevice BIdentifier
// Return the local MAC addy // Courtesy of FreeBSD hackers email list // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb. - (NSString *) macaddress{ int mib[6]; size_t len; char *buf; unsigned char *ptr; struct if_msghdr *ifm; struct sockaddr_dl *sdl; mib[0] = CTL_NET; mib[1] = AF_ROUTE; mib[2] = 0; mib[3] = AF_LINK; mib[4] = NET_RT_IFLIST; if ((mib[5] = if_nametoindex("en0")) == 0) { printf("Error: if_nametoindex error\n"); return NULL; } if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { printf("Error: sysctl, take 1\n"); return NULL; } if ((buf = malloc(len)) == NULL) { printf("Could not allocate memory. error!\n"); return NULL; } if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { printf("Error: sysctl, take 2"); free(buf); return NULL; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)]; free(buf); return outstring; }
7> Jøhn Nârrøw..:现在iOS 7设备 - 总是返回02:00:00:00:00:00的MAC地址.
所以最好使用[UIDevice identifierForVendor].
所以最好调用此方法来获取应用程序特定的唯一键
类别会更合适
导入"UIDevice + Identifier.h"
- (NSString *) identifierForVendor1 { if ([[UIDevice currentDevice] respondsToSelector:@selector(identifierForVendor)]) { return [[[UIDevice currentDevice] identifierForVendor] UUIDString]; } return @""; }现在调用上面的方法来获取唯一的地址
NSString *like_UDID=[NSString stringWithFormat:@"%@", [[UIDevice currentDevice] identifierForVendor1]]; NSLog(@"%@",like_UDID);