获取mac地址
#include<sys/socket.h>//Permsqr
#include<sys/sysctl.h>
#include<net/if.h>
#include<net/if_dl.h>
#pragmamarkMAC
//ReturnthelocalMACaddy
//CourtesyofFreeBSDhackersemaillist
//Accidentallymungedduringpreviousupdate.Fixedthankstomlamb.
-(NSString*)macaddress
{
intmib[6];
size_tlen;
char*buf;
unsignedchar*ptr;
structif_msghdr*ifm;
structsockaddr_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_nametoindexerror/n");
returnNULL;
}
if(sysctl(mib,6,NULL,&len,NULL,0)<0){
printf("Error:sysctl,take1/n");
returnNULL;
}
if((buf=malloc(len))==NULL){
printf("Couldnotallocatememory.error!/n");
returnNULL;
}
if(sysctl(mib,6,buf,&len,NULL,0)<0){
printf("Error:sysctl,take2");
returnNULL;
}
ifm=(structif_msghdr*)buf;
sdl=(structsockaddr_dl*)(ifm+1);
ptr=(unsignedchar*)LLADDR(sdl);
//NSString*outstring=[NSStringstringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5)];
NSString*outstring=[NSStringstringWithFormat:@"%02x%02x%02x%02x%02x%02x",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5)];
free(buf);
return[outstringuppercaseString];
}