获取iPhone本机IP地址新方法

最近在项目中要获取iphone手机本机的ip地址。在我一博客中也写到方法了。但是那种方法只适合于非3G网络。今天网上找了下一个不同以前的获取方式。特记录下。先感谢作者。 转至 http://mobile.51cto.com/iphone-282795.htm

获取iPhone本机IP地址并且不需调用私有API方法是本文要介绍的内容,主要是手头一个iphone项目需要取iphone本机ip地址,在iphone os 2.0上可以用下面的方法获得。内容不多,主要是代码实现IP地址的获取。

 
 
  1. -(NSString*)getAddress {  
  2. char iphone_ip[255];  
  3. strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
  4. NSHost* myhost = [NSHost currentHost];  
  5. if (myhost)  
  6. {  
  7.     NSString *ad = [myhost address];  
  8.     if (ad)  
  9.         strcpy(iphone_ip,[ad cStringUsingEncoding:NSISOLatin1StringEncoding]);  
  10. }  
  11.       return [NSString stringWithFormat:@"%s",iphone_ip];   
  12. }  
  13. 到3.0这个方法成了苹果私有api了,用了不对不说,error:  
  14.  
  15. warning: no ‘+currentHost’ method found  
  16. warning: (Messages without a matching method signature)  
  17.  
  18. ,提交的app还被reject:  
  19.  
  20. [NSHost currentHost] will also work, but it is deprecated and considered a “Private API” by Apple, 
  21. so you won’t be able to submit your application to App Store.  
  22.  
  23. google很久无果;今天无意发现一个老外的blog贴了方法,试用了下完全OK,要翻墙看,转载记录一下.  
  24.  
  25. As far as I know there is only one hacky way to do that. You basically open a socket and get its address using POSIX functions. 
  26. Here is the code I used for this:  
  27.  
  28. /*  
  29.  *  IPAdress.h  
  30.  *  
  31.  *  
  32.  */  
  33.    
  34. #define MAXADDRS    32  
  35.    
  36. extern char *if_names[MAXADDRS];  
  37. extern char *ip_names[MAXADDRS];  
  38. extern char *hw_addrs[MAXADDRS];  
  39. extern unsigned long ip_addrs[MAXADDRS];  
  40.    
  41. // Function prototypes  
  42.    
  43. void InitAddresses();  
  44. void FreeAddresses();  
  45. void GetIPAddresses();  
  46. void GetHWAddresses();  
  47.    
  48.    
  49. /*  
  50.  *  IPAddress.c  
  51.  *  
  52.  */  
  53.    
  54. #include "IPAddress.h"  
  55.    
  56. #include <stdio.h> 
  57. #include <stdlib.h> 
  58. #include <string.h> 
  59. #include <unistd.h> 
  60. #include <sys/ioctl.h> 
  61. #include <sys/types.h> 
  62. #include <sys/socket.h> 
  63. #include <netinet/in.h> 
  64. #include <netdb.h> 
  65. #include <arpa/inet.h> 
  66. #include <sys/sockio.h> 
  67. #include <net/if.h> 
  68. #include <errno.h> 
  69. #include <net/if_dl.h> 
  70.     
  71. #define    min(a,b)    ((a) < (b) ? (a) : (b))  
  72. #define    max(a,b)    ((a) > (b) ? (a) : (b))  
  73.    
  74. #define BUFFERSIZE    4000  
  75.    
  76. char *if_names[MAXADDRS];  
  77. char *ip_names[MAXADDRS];  
  78. char *hw_addrs[MAXADDRS];  
  79. unsigned long ip_addrs[MAXADDRS];  
  80.    
  81. static int   nextAddr = 0;  
  82.    
  83. void InitAddresses()  
  84. {  
  85.     int i;  
  86.     for (i=0; i<MAXADDRS; ++i)  
  87.     {  
  88.         if_names[i] = ip_names[i] = hw_addrs[i] = NULL;  
  89.         ip_addrs[i] = 0;  
  90.     }  
  91. }  
  92.    
  93. void FreeAddresses()  
  94. {  
  95.     int i;  
  96.     for (i=0; i<MAXADDRS; ++i)  
  97.     {  
  98.         if (if_names[i] != 0) free(if_names[i]);  
  99.         if (ip_names[i] != 0) free(ip_names[i]);  
  100.         if (hw_addrs[i] != 0) free(hw_addrs[i]);  
  101.         ip_addrs[i] = 0;  
  102.     }  
  103.     InitAddresses();  
  104. }  
  105.    
  106. void GetIPAddresses()  
  107. {  
  108.     int                 i, len, flags;  
  109.     char                buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;  
  110.     struct ifconf       ifc;  
  111.     struct ifreq        *ifr, ifrcopy;  
  112.     struct sockaddr_in    *sin;  
  113.       
  114.     char temp[80];  
  115.       
  116.     int sockfd;  
  117.       
  118.     for (i=0; i<MAXADDRS; ++i)  
  119.     {  
  120.         if_names[i] = ip_names[i] = NULL;  
  121.         ip_addrs[i] = 0;  
  122.     }  
  123.       
  124.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  125.     if (sockfd < 0)  
  126.     {  
  127.         perror("socket failed");  
  128.         return;  
  129.     }  
  130.       
  131.     ifc.ifc_len = BUFFERSIZE;  
  132.     ifc.ifc_buf = buffer;  
  133.       
  134.     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)  
  135.     {  
  136.         perror("ioctl error");  
  137.         return;  
  138.     }  
  139.       
  140.     lastname[0] = 0;  
  141.       
  142.     for (ptr = buffer; ptr < buffer + ifc.ifc_len; )  
  143.     {  
  144.         ifr = (struct ifreq *)ptr;  
  145.         len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);  
  146.         ptr += sizeof(ifr->ifr_name) + len;    // for next one in buffer  
  147.           
  148.         if (ifr->ifr_addr.sa_family != AF_INET)  
  149.         {  
  150.             continue;    // ignore if not desired address family  
  151.         }  
  152.           
  153.         if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)  
  154.         {  
  155.             *cptr = 0;        // replace colon will null  
  156.         }  
  157.           
  158.         if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)  
  159.         {  
  160.             continue;    /* already processed this interface */  
  161.         }  
  162.           
  163.         memcpy(lastname, ifr->ifr_name, IFNAMSIZ);  
  164.           
  165.         ifrcopy = *ifr;  
  166.         ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);  
  167.         flags = ifrcopy.ifr_flags;  
  168.         if ((flags & IFF_UP) == 0)  
  169.         {  
  170.             continue;    // ignore if interface not up  
  171.         }  
  172.           
  173.         if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);  
  174.         if (if_names[nextAddr] == NULL)  
  175.         {  
  176.             return;  
  177.         }  
  178.         strcpy(if_names[nextAddr], ifr->ifr_name);  
  179.           
  180.         sin = (struct sockaddr_in *)&ifr->ifr_addr;  
  181.         strcpy(temp, inet_ntoa(sin->sin_addr));  
  182.           
  183.         ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);  
  184.         if (ip_names[nextAddr] == NULL)  
  185.         {  
  186.             return;  
  187.         }  
  188.         strcpy(ip_names[nextAddr], temp);  
  189.           
  190.         ip_addrs[nextAddr] = sin->sin_addr.s_addr;  
  191.           
  192.         ++nextAddr;  
  193.     }  
  194.       
  195.     close(sockfd);  
  196. }  
  197.    
  198. void GetHWAddresses()  
  199. {  
  200.     struct ifconf ifc;  
  201.     struct ifreq *ifr;  
  202.     int i, sockfd;  
  203.     char buffer[BUFFERSIZE], *cp, *cplim;  
  204.     char temp[80];  
  205.       
  206.     for (i=0; i<MAXADDRS; ++i)  
  207.     {  
  208.         hw_addrs[i] = NULL;  
  209.     }  
  210.       
  211.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);  
  212.     if (sockfd < 0)  
  213.     {  
  214.         perror("socket failed");  
  215.         return;  
  216.     }  
  217.       
  218.     ifc.ifc_len = BUFFERSIZE;  
  219.     ifc.ifc_buf = buffer;  
  220.       
  221.     if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)  
  222.     {  
  223.         perror("ioctl error");  
  224.         close(sockfd);  
  225.         return;  
  226.     }  
  227.       
  228.     ifr = ifc.ifc_req;  
  229.       
  230.     cplim = buffer + ifc.ifc_len;  
  231.       
  232.     for (cp=buffer; cp < cplim; )  
  233.     {  
  234.         ifr = (struct ifreq *)cp;  
  235.         if (ifr->ifr_addr.sa_family == AF_LINK)  
  236.         {  
  237.             struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;  
  238.             int a,b,c,d,e,f;  
  239.             int i;  
  240.               
  241.             strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));  
  242.             sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);  
  243.             sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);  
  244.               
  245.             for (i=0; i<MAXADDRS; ++i)  
  246.             {  
  247.                 if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name,if_names[i]) == 0))  
  248.                 {  
  249.                     if (hw_addrs[i] == NULL)  
  250.                     {  
  251.                         hw_addrs[i] = (char *)malloc(strlen(temp)+1);  
  252.                         strcpy(hw_addrs[i], temp);  
  253.                         break;  
  254.                     }  
  255.                 }  
  256.             }  
  257.         }  
  258.         cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);  
  259.     }  
  260.       
  261.     close(sockfd);  
  262. }  
  263. test:  
  264.  
  265. #import "IPAdress.h"  
  266.  
  267. - (NSString *)deviceIPAdress {  
  268.     InitAddresses();  
  269.     GetIPAddresses();  
  270.     GetHWAddresses();  
  271.     return [NSString stringWithFormat:@"%s", ip_names[1]];  
  272. }  
  273.    
  274. - (void)viewDidLoad {  
  275.     [super viewDidLoad];  
  276.    
  277.     NSString* ip_iphone = [self deviceIPAdress];  
  278.    NSLog(@"ip:%@",ip_iphone);  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更这些源码资源,以适应各平台技术的最发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值