if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { printf("Error: sysctl, take 1/n"); return ; }
if ((buf = malloc(len)) == NULL) { printf("Could not allocate memory. error!/n"); return ; }
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { printf("Error: sysctl, take 2"); free(buf); return ; }
ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned char *)LLADDR(sdl); memcpy(mac,ptr, 6); free(buf); } 段代码可以良好的工作,直到iOS7的出现。不知出于什么原因,苹果对于sysctl和ioctl进行了技术处理,让MAC地址返回02:00:00:00:00:00。官方文档上这样写的“Twolow-level networking APIs that used to return a MAC address now return thefixed value 02:00:00:00:00:00. The APIs in question are sysctl(NET_RT_IFLIST) andioctl(SIOCGIFCONF). Developers using the value of the MAC address should migrate toidentifiers such as -[UIDeviceidentifierForVendor].This change affects all apps running on iOS 7”