iOS get MAC Address

Source: Internet
Author: User

#include//per MSQR

#include

#include

#include

#pragma Mark MAC

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 ("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:@ "x:x:x:x:x:x", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (PTR+4), * (ptr+5 )];

NSString *outstring = [NSString stringwithformat:@ "xxxxxx", *ptr, * (ptr+1), * (ptr+2), * (ptr+3), * (PTR+4), * (ptr+5)];

Free (BUF);

return [outstring uppercasestring];

}

Transfer from http://blog.csdn.net/doubleuto/article/details/6599516

iOS get MAC Address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.