xcode获取mac address
1,首先下载GetPrimaryMACAddress.c:https://files.cnblogs.com/files/MartinLi841538513/GetPrimaryMACAddress.c.zip
2,然后设置GetPrimaryMACAddress.c的Type为Objective-C Source,否则会出现头文件互相引用问题。
3,在你想要获取addres的地方,#include "GetPrimaryMACAddress.c"
并调用:
-(NSString *)macAddress{ NSMutableString *macAddress = [NSMutableString new]; kern_return_t kernResult = KERN_SUCCESS; io_iterator_t intfIterator; UInt8 MACAddress[kIOEthernetAddressSize]; kernResult = FindEthernetInterfaces(&intfIterator); if (KERN_SUCCESS != kernResult) { printf("FindEthernetInterfaces returned 0x%08x\n", kernResult); } else { kernResult = GetMACAddress(intfIterator, MACAddress, sizeof(MACAddress)); if (KERN_SUCCESS != kernResult) { printf("GetMACAddress returned 0x%08x\n", kernResult); } else { printf("This system's built-in MAC address is %02x:%02x:%02x:%02x:%02x:%02x.\n", MACAddress[0], MACAddress[1], MACAddress[2], MACAddress[3], MACAddress[4], MACAddress[5]); } } (void) IOObjectRelease(intfIterator); // Release the iterator. [macAddress appendFormat:@"%hhu:",MACAddress[0]]; [macAddress appendFormat:@"%hhu:",MACAddress[1]]; [macAddress appendFormat:@"%hhu:",MACAddress[2]]; [macAddress appendFormat:@"%hhu:",MACAddress[3]]; [macAddress appendFormat:@"%hhu:",MACAddress[4]]; [macAddress appendFormat:@"%hhu",MACAddress[5]]; return macAddress; }
然后,就会出来类似这样的macAddress:128:230:80:30:10:204 地址