#include//per MSQR
#include
#include
#include
#pragma Mark MAC
Return The local MAC Addy
Courtesy of FreeBSD Hackers Email list
Accidentally munged during previous update. Fixed thanks to 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");
return NULL;
}
IFM = (struct IF_MSGHDR *) buf;
SDL = (struct SOCKADDR_DL *) (IFM + 1);
ptr = (unsigned char *) lladdr (SDL);
NSString *outstring = [NSString stringwithformat:@ "x:x:x:x:x:x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (PTR+4), * (ptr+5 )];
NSString *outstring = [NSString stringwithformat:@ "xxxxxx", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (PTR+4), * (ptr+5)];
Free (BUF);
return [outstring uppercasestring];
}
Transfer from http://blog.csdn.net/doubleuto/article/details/6599516
iOS get MAC Address