First of all, the following two methods can get the phone's MAC address, but there is a limit, in the iOS7 below can be obtained. IOS7 after the Apple for SYSCTL and IOCTL technical processing, MAC address returned are 02:00:00:00:00:00.
It is written in the official document:
"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) and IOCTL (siocgifconf). Developers using the value of the MAC address should migrate toidentifiers such as-[uidevice Identifierforvendor]. This is affects all apps running on IOS 7 ".
So after IOS7 want to get the device's only label MAC address has been dead, can only be replaced by other.
Here are two ways:
Need to import several header files
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>
Method 1:
Return to the local MAC addy//courtesy of FreeBSD Hackers email list//accidentally munged during-previous update.
Fixed 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:@ "%02x:%02x:%02x:%02x:%02x:%02x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3),
* (PTR+4), * (ptr+5)]; NSString *outstring = [NSString stringwithformat:@ "%02x%02x%02x%02x%02x%02x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (P
TR+4), * (ptr+5)];
NSLog (@ "outstring:%@", outstring);
Free (BUF);
return [outstring uppercasestring]; }
Method 2:
-(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 the all configured interfaces requested, get handle index if (mgmtinfo
BASE[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, NU
LL, 0) < 0) Errorflag = @ "Sysctl mgmtinfobase failure"; else {//Alloc Memory based on above call if ((Msgbuffer = malloc (length)) = = NULL) Errorflag = @ "Buffer allocation fail
Ure "; else {//Get system information, store in buffer if (Sysctl (Mgmtinfobase, 6, Msgbuffer, &len
Gth, 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 a array memcpy (&macaddress, Socketstruct->sdl_data + Soc
Ketstruct->sdl_nlen, 6); Read from char array into a string object, to traditional MAC address format nsstring *macaddressstring = [Nsstrin G Stringwithformat:@ "%02x:%02x:%02x:%02x:%02x:%02x", Macaddress[0], macaddress[1], macaddress[2],
MACADDRESS[3], macaddress[4], macaddress[5];
NSLog (@ "MAC address:%@", macaddressstring);
Release of the buffer memory free (msgbuffer);
return macaddressstring;
}
The above is iOS access to the phone's MAC address two ways, I hope to help you learn.