Last active
December 14, 2015 09:59
-
-
Save wsidell/5069159 to your computer and use it in GitHub Desktop.
iPhone Get MAC Address without network connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <net/if_dl.h> | |
#include <ifaddrs.h> | |
#define IFT_ETHER 0x6 | |
- (NSString *) macaddress{ | |
int success; | |
struct ifaddrs * addrs; | |
struct ifaddrs * cursor; | |
const struct sockaddr_dl * dlAddr; | |
const unsigned char* base; | |
int i; | |
char* macAddress= (char*)calloc(18, sizeof(char)); | |
char ifName[] = "en0"; | |
success = getifaddrs(&addrs) == 0; | |
if (success) { | |
cursor = addrs; | |
while (cursor != 0) { | |
if ( (cursor->ifa_addr->sa_family == AF_LINK) | |
&& (((const struct sockaddr_dl *) cursor->ifa_addr)->sdl_type == IFT_ETHER) | |
&& strcmp(ifName, cursor->ifa_name) == 0 ) { | |
dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr; | |
base = (const unsigned char*) &dlAddr->sdl_data[dlAddr->sdl_nlen]; | |
strcpy(macAddress, ""); | |
for (i = 0; i < dlAddr->sdl_alen; i++) { | |
if (i != 0) { | |
strcat(macAddress, ":"); | |
} | |
char partialAddr[3]; | |
sprintf(partialAddr, "%02X", base[i]); | |
strcat(macAddress, partialAddr); | |
} | |
} | |
cursor = cursor->ifa_next; | |
} | |
freeifaddrs(addrs); | |
} | |
NSString* mac= [[[NSString alloc] initWithCString:macAddress | |
encoding:NSMacOSRomanStringEncoding] autorelease]; | |
free(macAddress); | |
return mac; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iOS 7 no longer allows you to access the Mac Address on the device. The only solution is to use the advertising identifier.