iOS开发-如何获取当前设备的IP地址和MAC地址实操指导!

获取当前设备的IP地址和MAC地址:

1.导入相关库文件。

2.调用方法getIPAddress 调用方法getMacAddress。

  1. //
  2. // GetForIPMac.m
  3. // Eric
  4. //
  5. // Created by Eric on 15-3-24.
  6. // Copyright (c) 2015年 yons. All rights reserved.
  7. //
  8.  
  9. #import "GetForIPMac.h"
  10.  
  11. //Get IP 需要导入的库文件
  12.  
  13. #import <ifaddrs.h>
  14.  
  15. #import <arpa/inet.h>
  16.  
  17.  
  18. //Get MAC 需要导入的库文件
  19.  
  20. #include <sys/socket.h> // Per msqr
  21.  
  22. #include <sys/sysctl.h>
  23.  
  24. #include <net/if.h>
  25.  
  26. #include <net/if_dl.h>
  27.  
  28.  
  29. @interface GetForIPMac ()
  30.  
  31. @end
  32.  
  33. @implementation GetForIPMac
  34.  
  35. - (void)viewDidLoad {
  36.  
  37. [super viewDidLoad];
  38. // Do any additional setup after loading the view.
  39.  
  40. //获取当前设备的IP和MAC地址
  41.  
  42. NSString *ip_str = [self getIPAddress];
  43.  
  44. NSLog(@" Get IP Address %@",ip_str);//192.168.191.5
  45.  
  46. NSString *mac_str = [self getMacAddress];
  47.  
  48. NSLog(@"Get MAC Address %@",mac_str); //B4:F0:AB:1C:05:93
  49. }
  50.  
  51. #pragma mark IP
  52. /**
  53. * @Author, 15-03-24 09:07:06
  54. *
  55. * Get IP Address
  56. *
  57. * #import <ifaddrs.h>
  58. *
  59. * #import <arpa/inet.h>
  60. *
  61. * @return
  62. */
  63.  
  64. - (NSString *)getIPAddress {
  65.  
  66. NSString *address = @"error";
  67.  
  68. struct ifaddrs *interfaces = NULL;
  69.  
  70. struct ifaddrs *temp_addr = NULL;
  71.  
  72. int success = 0;
  73.  
  74. // retrieve the current interfaces - returns 0 on success
  75.  
  76. success = getifaddrs(&interfaces);
  77.  
  78. if (success == 0) {
  79.  
  80. // Loop through linked list of interfaces
  81.  
  82. temp_addr = interfaces;
  83.  
  84. while(temp_addr != NULL) {
  85.  
  86. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  87.  
  88. // Check if interface is en0 which is the wifi connection on the iPhone
  89.  
  90. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  91.  
  92. // Get NSString from C String
  93.  
  94. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  95.  
  96. }
  97.  
  98. }
  99.  
  100. temp_addr = temp_addr->ifa_next;
  101. }
  102.  
  103. }
  104.  
  105. // Free memory
  106.  
  107. freeifaddrs(interfaces);
  108.  
  109. return address;
  110. }
  111.  
  112. #pragma mark MAC
  113.  
  114. /**
  115.  
  116. * @Author , 15-03-24 09:07:06
  117.  
  118. *
  119.  
  120. * #include <sys/socket.h> // Per msqr
  121.  
  122. * #include <sys/sysctl.h>
  123.  
  124. * #include <net/if.h>
  125.  
  126. * #include <net/if_dl.h>
  127.  
  128. *
  129.  
  130. * Return the local MAC addy
  131.  
  132. * Courtesy of FreeBSD hackers email list
  133.  
  134. * Accidentally munged during previous update. Fixed thanks to mlamb.
  135.  
  136. *
  137.  
  138. * @return
  139.  
  140. */
  141.  
  142. - (NSString *) getMacAddress
  143. {
  144. int mib[6];
  145.  
  146. size_t len;
  147.  
  148. charchar *buf;
  149.  
  150. unsigned charchar *ptr;
  151.  
  152. struct if_msghdr *ifm;
  153.  
  154. struct sockaddr_dl *sdl;
  155.  
  156.  
  157. mib[0] = CTL_NET;
  158.  
  159. mib[1] = AF_ROUTE;
  160.  
  161. mib[2] = 0;
  162.  
  163. mib[3] = AF_LINK;
  164.  
  165. mib[4] = NET_RT_IFLIST;
  166.  
  167.  
  168. if ((mib[5] = if_nametoindex("en0")) == 0) {
  169.  
  170. printf("Error: if_nametoindex error/n");
  171.  
  172. return NULL;
  173.  
  174. }
  175.  
  176.  
  177. if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
  178.  
  179. printf("Error: sysctl, take 1/n");
  180.  
  181. return NULL;
  182.  
  183. }
  184.  
  185. if ((buf = malloc(len)) == NULL) {
  186.  
  187. printf("Could not allocate memory. error!/n");
  188.  
  189. return NULL;
  190.  
  191. }
  192.  
  193.  
  194.  
  195. if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
  196.  
  197. printf("Error: sysctl, take 2");
  198.  
  199. return NULL;
  200.  
  201. }
  202.  
  203.  
  204. ifm = (struct if_msghdr *)buf;
  205.  
  206. sdl = (struct sockaddr_dl *)(ifm + 1);
  207.  
  208. ptr = (unsigned charchar *)LLADDR(sdl);
  209.  
  210. NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
  211.  
  212. free(buf);
  213.  
  214. return [outstring uppercaseString];
  215.  
  216. }
  217.  
  218.  
  219. - (void)didReceiveMemoryWarning {
  220.  
  221. [super didReceiveMemoryWarning];
  222. // Dispose of any resources that can be recreated.
  223.  
  224.  
  225. }
  226.  
  227. @end

推荐一家即时通讯云服务商:yun2win:www.yun2win.com

转载于:https://my.oschina.net/cainiaodudu/blog/739871

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值