
下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
// // GetForIPMac.m // Eric // // Created by Eric on 15-3-24. // copyright (c) 2015年 yons. All rights reserved. // #import "GetForIPMac.h" //Get IP 需要导入的库文件 #import <ifaddrs.h> #import <arpa/inet.h> //Get MAC 需要导入的库文件 #include <sys/socket.h> // Per msqr #include <sys/sysctl.h> #include <net/if.h> #include <net/if_dl.h> @interface GetForIPMac () @end @implementation GetForIPMac - (voID)vIEwDIDLoad { [super vIEwDIDLoad]; // Do any additional setup after loading the vIEw. //获取当前设备的IP和MAC地址 Nsstring *ip_str = [self getIPAddress]; NSLog(@" Get IP Address %@",ip_str);//192.168.191.5 Nsstring *mac_str = [self getMacAddress]; NSLog(@"Get MAC Address %@",mac_str); //B4:F0:AB:1C:05:93 } #pragma mark IP /** * @Author,15-03-24 09:07:06 * * Get IP Address * * #import <ifaddrs.h> * * #import <arpa/inet.h> * * @return */ - (Nsstring *)getIPAddress { Nsstring *address = @"error"; struct ifaddrs *interfaces = NulL; struct ifaddrs *temp_addr = NulL; int success = 0; // retrIEve the current interfaces - returns 0 on success success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked List of interfaces temp_addr = interfaces; while(temp_addr != NulL) { if(temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone if([[Nsstring stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get Nsstring from C String address = [Nsstring stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; } } temp_addr = temp_addr->ifa_next; } } // Free memory freeifaddrs(interfaces); return address; } #pragma mark MAC /** * @Author,15-03-24 09:07:06 * * #include <sys/socket.h> // Per msqr * #include <sys/sysctl.h> * #include <net/if.h> * #include <net/if_dl.h> * * Return the local MAC addy * Courtesy of FreeBSD Hackers email List * AccIDentally munged during prevIoUs update. Fixed thanks to mlamb. * * @return */ - (Nsstring *) getMacAddress { int mib[6]; size_t len; charchar *buf; unsigned charchar *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,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,buf,take 2"); return NulL; } ifm = (struct if_msghdr *)buf; sdl = (struct sockaddr_dl *)(ifm + 1); ptr = (unsigned charchar *)LLADDR(sdl); Nsstring *outstring = [Nsstring stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5)]; free(buf); return [outstring uppercaseString]; } - (voID)dIDReceiveMemoryWarning { [super dIDReceiveMemoryWarning]; // dispose of any resources that can be recreated. } @end 以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS获取当前设备的IP地址和MAC地址全部内容,希望文章能够帮你解决iOS获取当前设备的IP地址和MAC地址所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)