Skip to content

Instantly share code, notes, and snippets.

@wsidell
Last active December 14, 2015 09:59
Show Gist options
  • Save wsidell/5069159 to your computer and use it in GitHub Desktop.
Save wsidell/5069159 to your computer and use it in GitHub Desktop.
iPhone Get MAC Address without network connection
#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;
}
@wsidell
Copy link
Author

wsidell commented Aug 22, 2013

iOS 7 no longer allows you to access the Mac Address on the device. The only solution is to use the advertising identifier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment