获取iPhone本机IP地址非调用私有API方法

移动开发 iOS
本文介绍的是如何获取iPhone本机IP地址并且不需调用私有API方法,文中很详细的讲解了IP地址是如何获取的,来看详细内容。

获取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);  

小结:获取iPhone本机IP地址非调用私有API方法的内容介绍完了,希望本文能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2009-12-04 08:48:38

FAST路由器IP地址

2011-09-26 09:54:40

perl获取本机ip

2009-06-19 17:31:59

Java获取IP地址

2020-11-30 12:15:26

KubernetesPodLinux

2011-08-05 16:58:25

iPad iPhone UIImagePic

2011-07-25 15:46:10

iPhone 动态

2011-11-30 15:43:40

JavaJSP

2010-06-13 14:26:22

IP协议地址

2009-09-18 19:21:17

C#接口

2024-04-15 05:00:00

kubernete网络容器

2009-12-07 09:31:23

Linux系统调用表地址

2009-07-24 10:38:35

ASP.NET获取MA

2022-10-10 09:13:09

本机函数汇编代码

2018-11-29 13:50:02

APIAPP数据集

2009-02-18 10:17:00

宽带网络IP地址

2018-05-14 10:56:36

MySQL数据库存储

2020-09-16 18:27:36

Linux方法IP地址

2019-08-22 07:24:25

2015-02-11 16:25:25

微信SDK

2015-07-10 09:08:52

IP地址IP地址冲突
点赞
收藏

51CTO技术栈公众号