免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1386 | 回复: 0
打印 上一主题 下一主题

[iOS] 获取 iOS 设备当前 ip 地址 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-04 11:43 |只看该作者 |倒序浏览
http://blog.csdn.net/favormm/article/details/6858330
用到了ASIHTTPRequest这个网络开源库。
http://automation.whatismyip.com/n09230945.asp这个http url就可以获取你当前的ip地址。
如果在内网就会获取到内网IP
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5. #include <sys/ioctl.h>  
  6. #include <sys/types.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <netdb.h>  
  10. #include <arpa/inet.h>  
  11. #include <sys/sockio.h>  
  12. #include <net/if.h>  
  13. #include <errno.h>  
  14. #include <net/if_dl.h>  
  15.    
  16. //#include "GetAddresses.h"  
  17.    
  18. #define min(a,b)    ((a) < (b) ? (a) : (b))  
  19. #define max(a,b)    ((a) > (b) ? (a) : (b))  
  20.    
  21. #define BUFFERSIZE  4000  
  22.    
  23. char *if_names[MAXADDRS];  
  24. char *ip_names[MAXADDRS];  
  25. char *hw_addrs[MAXADDRS];  
  26. unsigned long ip_addrs[MAXADDRS];  
  27.    
  28. static int   nextAddr = 0;  
  29.    
  30. void InitAddresses()  
  31. {  
  32.     int i;  
  33.     for (i=0; i<MAXADDRS; ++i)  
  34.     {  
  35.         if_names[i] = ip_names[i] = hw_addrs[i] = NULL;  
  36.         ip_addrs[i] = 0;  
  37.     }  
  38. }  
  39.    
  40. void FreeAddresses()  
  41. {  
  42.     int i;  
  43.     for (i=0; i<MAXADDRS; ++i)  
  44.     {  
  45.         if (if_names[i] != 0) free(if_names[i]);  
  46.         if (ip_names[i] != 0) free(ip_names[i]);  
  47.         if (hw_addrs[i] != 0) free(hw_addrs[i]);  
  48.         ip_addrs[i] = 0;  
  49.     }  
  50.     InitAddresses();  
  51. }  
  52.    
  53. void GetIPAddresses()  
  54. {  
  55.     int                 i, len, flags;  
  56.     char                buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  
  57.     struct ifconf       ifc;  
  58.     struct ifreq        *ifr, ifrcopy;  
  59.     struct sockaddr_in  *sin;  
  60.       
  61.     char temp[80];  
  62.       
  63.     int sockfd;  
  64.       
  65.     for (i=0; i<MAXADDRS; ++i)  
  66.     {  
  67.         if_names[i] = ip_names[i] = NULL;  
  68.         ip_addrs[i] = 0;  
  69.     }  
  70.       
  71.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  72.     if (sockfd < 0)  
  73.     {  
  74.         perror("socket failed");  
  75.         return;  
  76.     }  
  77.       
  78.     ifc.ifc_len = BUFFERSIZE;  
  79.     ifc.ifc_buf = buffer;  
  80.       
  81.     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)  
  82.     {  
  83.         perror("ioctl error");  
  84.         return;  
  85.     }  
  86.       
  87.     lastname[0] = 0;  
  88.       
  89.     for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  
  90.     {  
  91.         ifr = (struct ifreq *)ptr;  
  92.         len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);  
  93.         ptr += sizeof(ifr->ifr_name) + len;  // for next one in buffer  
  94.            
  95.         if (ifr->ifr_addr.sa_family != AF_INET)  
  96.         {  
  97.             continue;   // ignore if not desired address family  
  98.         }  
  99.            
  100.         if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)  
  101.         {  
  102.             *cptr = 0;      // replace colon will null  
  103.         }  
  104.            
  105.         if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)  
  106.         {  
  107.             continue;   /* already processed this interface */
  108.         }  
  109.            
  110.         memcpy(lastname, ifr->ifr_name, IFNAMSIZ);  
  111.            
  112.         ifrcopy = *ifr;  
  113.         ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);  
  114.         flags = ifrcopy.ifr_flags;  
  115.         if ((flags & IFF_UP) == 0)  
  116.         {  
  117.             continue;   // ignore if interface not up  
  118.         }  
  119.            
  120.         if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);  
  121.         if (if_names[nextAddr] == NULL)  
  122.         {  
  123.             return;  
  124.         }  
  125.         strcpy(if_names[nextAddr], ifr->ifr_name);  
  126.            
  127.         sin = (struct sockaddr_in *)&ifr->ifr_addr;  
  128.         strcpy(temp, inet_ntoa(sin->sin_addr));  
  129.            
  130.         ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);  
  131.         if (ip_names[nextAddr] == NULL)  
  132.         {  
  133.             return;  
  134.         }  
  135.         strcpy(ip_names[nextAddr], temp);  
  136.            
  137.         ip_addrs[nextAddr] = sin->sin_addr.s_addr;  
  138.            
  139.         ++nextAddr;  
  140.     }  
  141.       
  142.     close(sockfd);  
  143. }  
  144.    
  145. void GetHWAddresses()  
  146. {  
  147.     struct ifconf ifc;  
  148.     struct ifreq *ifr;  
  149.     int i, sockfd;  
  150.     char buffer[BUFFERSIZE], *cp, *cplim;  
  151.     char temp[80];  
  152.       
  153.     for (i=0; i<MAXADDRS; ++i)  
  154.     {  
  155.         hw_addrs[i] = NULL;  
  156.     }  
  157.       
  158.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  159.     if (sockfd < 0)  
  160.     {  
  161.         perror("socket failed");  
  162.         return;  
  163.     }  
  164.       
  165.     ifc.ifc_len = BUFFERSIZE;  
  166.     ifc.ifc_buf = buffer;  
  167.       
  168.     if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)  
  169.     {  
  170.         perror("ioctl error");  
  171.         close(sockfd);  
  172.         return;  
  173.     }  
  174.       
  175.     ifr = ifc.ifc_req;  
  176.       
  177.     cplim = buffer + ifc.ifc_len;  
  178.       
  179.     for (cp=buffer; cp < cplim; )  
  180.     {  
  181.         ifr = (struct ifreq *)cp;  
  182.         if (ifr->ifr_addr.sa_family == AF_LINK)  
  183.         {  
  184.             struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;  
  185.             int a,b,c,d,e,f;  
  186.             int i;  
  187.                
  188.             strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));  
  189.             sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);  
  190.             sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);  
  191.                
  192.             for (i=0; i<MAXADDRS; ++i)  
  193.             {  
  194.                 if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))  
  195.                 {  
  196.                     if (hw_addrs[i] == NULL)  
  197.                     {  
  198.                         hw_addrs[i] = (char *)malloc(strlen(temp)+1);  
  199.                         strcpy(hw_addrs[i], temp);  
  200.                         break;  
  201.                     }  
  202.                 }  
  203.             }  
  204.         }  
  205.         cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);  
  206.     }  
  207.       
  208.     close(sockfd);  
  209. }
复制代码
[C/C++]代码
  1. - (NSString *)deviceIPAdress {  
  2.     InitAddresses();  
  3.     GetIPAddresses();  
  4.     GetHWAddresses();  
  5.    
  6.     return [NSString stringWithFormat:@"%s", ip_names[1]];  
  7. }
复制代码
第二种方法是可以获取公网ip

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP