First of all, the following two methods can obtain the mac address of the mobile phone, but there is a limit that can be obtained only on iOS. After iOS7, Apple performed technical processing on sysctl and ioctl. the MAC address returns 02: 00: 00: 00: 00: 00. The "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 shocould migrate toidentifiers such as-[UIDevice identifierForVendor]. this change affects all apps running on iOS 7 ".
Therefore, the unique Mac address of the device cannot be obtained after iOS7 and can only be replaced by another one.
The following two methods are described:
You need to import several header files.
[Html]View plaincopyprint?
- # Include
- # Include
- # Include
Method 1:
[Html]View plaincopyprint?
- // 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 (cocould 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), * (ptr + 4), * (ptr + 5)];
-
- NSLog (@ outString: % @, outstring );
-
- Free (buf );
-
- Return [outstring uppercaseString];
- }
From http://blog.csdn.net/showhilllee
Method 2:
[Html]View plaincopyprint?
- -(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: % 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;
- }