取iphone本机ip地址方法(非调用私有api方法)

  1. 私有方法:  
  2.   
  3. NSHost* myhost =[NSHost currentHost];  
  4. NSString *ad = [myhost address];  
  5.   
  6.    
  7.   
  8. 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. Here is the code I used for this:  
  9.   
  10. /* 
  11.  *  IPAdress.h 
  12.  * 
  13.  * 
  14.  */   
  15.    
  16. #define MAXADDRS     32   
  17.    
  18. extern char * if_names [ MAXADDRS ] ;  
  19. extern char * ip_names [ MAXADDRS ] ;  
  20. extern char * hw_addrs [ MAXADDRS ] ;  
  21. extern unsigned long ip_addrs [ MAXADDRS ] ;  
  22.    
  23. // Function prototypes   
  24.    
  25. void InitAddresses () ;  
  26. void FreeAddresses () ;  
  27. void GetIPAddresses () ;  
  28. void GetHWAddresses () ;  
  29.    
  30.    
  31. /* 
  32.  *  IPAddress.c 
  33.  * 
  34.  */   
  35.    
  36. #include " IPAddress.h "   
  37.    
  38. #include < stdio.h >   
  39. #include < stdlib.h >   
  40. #include < string.h >   
  41. #include < unistd.h >   
  42. #include < sys/ioctl.h >   
  43. #include < sys/types.h >   
  44. #include < sys/socket.h >   
  45. #include < netinet/in.h >   
  46. #include < netdb.h >   
  47. #include < arpa/inet.h >   
  48. #include < sys/sockio.h >   
  49. #include < net/if.h >   
  50. #include < errno.h >   
  51. #include < net/if_dl.h >   
  52.     
  53. #define     min ( a , b )     (( a ) < ( b ) ? ( a ) : ( b ))   
  54. #define     max ( a , b )     (( a ) > ( b ) ? ( a ) : ( b ))   
  55.    
  56. #define BUFFERSIZE     4000   
  57.    
  58. char * if_names [ MAXADDRS ] ;  
  59. char * ip_names [ MAXADDRS ] ;  
  60. char * hw_addrs [ MAXADDRS ] ;  
  61. unsigned long ip_addrs [ MAXADDRS ] ;  
  62.    
  63. static int     nextAddr = 0 ;  
  64.    
  65. void InitAddresses ()   
  66. {   
  67.      int i ;  
  68.      for ( i = 0 ; i < MAXADDRS ; ++ i )   
  69.      {   
  70.          if_names [ i ] = ip_names [ i ] = hw_addrs [ i ] = NULL ;  
  71.          ip_addrs [ i ] = 0 ;  
  72.      }   
  73. }   
  74.    
  75. void FreeAddresses ()   
  76. {   
  77.      int i ;  
  78.      for ( i = 0 ; i < MAXADDRS ; ++ i )   
  79.      {   
  80.          if ( if_names [ i ] != 0 ) free ( if_names [ i ]) ;  
  81.          if ( ip_names [ i ] != 0 ) free ( ip_names [ i ]) ;  
  82.          if ( hw_addrs [ i ] != 0 ) free ( hw_addrs [ i ]) ;  
  83.          ip_addrs [ i ] = 0 ;  
  84.      }   
  85.      InitAddresses () ;  
  86. }   
  87.    
  88. void GetIPAddresses ()   
  89. {   
  90.      int                   i , len , flags ;  
  91.      char                 buffer [ BUFFERSIZE ] , * ptr , lastname [ IFNAMSIZ ] , * cptr ;  
  92.      struct ifconf         ifc ;  
  93.      struct ifreq         * ifr , ifrcopy ;  
  94.      struct sockaddr_in     * sin ;  
  95.       
  96.      char temp [ 80 ] ;  
  97.       
  98.      int sockfd ;  
  99.       
  100.      for ( i = 0 ; i < MAXADDRS ; ++ i )   
  101.      {   
  102.          if_names [ i ] = ip_names [ i ] = NULL ;  
  103.          ip_addrs [ i ] = 0 ;  
  104.      }   
  105.       
  106.      sockfd = socket ( AF_INET , SOCK_DGRAM , 0 ) ;  
  107.      if ( sockfd < 0 )   
  108.      {   
  109.          perror ( " socket failed " ) ;  
  110.          return ;  
  111.      }   
  112.       
  113.      ifc . ifc_len = BUFFERSIZE ;  
  114.      ifc . ifc_buf = buffer ;  
  115.       
  116.      if ( ioctl ( sockfd , SIOCGIFCONF , & ifc ) < 0 )   
  117.      {   
  118.          perror ( " ioctl error " ) ;  
  119.          return ;  
  120.      }   
  121.       
  122.      lastname [ 0 ] = 0 ;  
  123.       
  124.      for ( ptr = buffer ; ptr < buffer + ifc . ifc_len ; )   
  125.      {   
  126.          ifr = ( struct ifreq * ) ptr ;  
  127.          len = max ( sizeof ( struct sockaddr ) , ifr -> ifr_addr . sa_len ) ;  
  128.          ptr += sizeof ( ifr -> ifr_name ) + len ;    // for next one in buffer   
  129.           
  130.          if ( ifr -> ifr_addr . sa_family != AF_INET )   
  131.          {   
  132.              continue ;    // ignore if not desired address family   
  133.          }   
  134.           
  135.          if (( cptr = ( char * ) strchr ( ifr -> ifr_name , ':' )) != NULL )   
  136.          {   
  137.             * cptr = 0 ;        // replace colon will null   
  138.          }   
  139.           
  140.          if ( strncmp ( lastname , ifr -> ifr_name , IFNAMSIZ ) == 0 )   
  141.          {   
  142.              continue ;    /* already processed this interface */   
  143.          }   
  144.           
  145.          memcpy ( lastname , ifr -> ifr_name , IFNAMSIZ ) ;  
  146.           
  147.          ifrcopy = * ifr ;  
  148.          ioctl ( sockfd , SIOCGIFFLAGS , & ifrcopy ) ;  
  149.          flags = ifrcopy . ifr_flags ;  
  150.          if (( flags & IFF_UP ) == 0 )   
  151.          {   
  152.              continue ;    // ignore if interface not up   
  153.          }   
  154.           
  155.          if_names [ nextAddr ] = ( char * ) malloc ( strlen ( ifr -> ifr_name ) + 1 ) ;  
  156.          if ( if_names [ nextAddr ] == NULL )   
  157.          {   
  158.              return ;  
  159.          }   
  160.          strcpy ( if_names [ nextAddr ] , ifr -> ifr_name ) ;  
  161.           
  162.          sin = ( struct sockaddr_in * ) & ifr -> ifr_addr ;  
  163.          strcpy ( temp , inet_ntoa ( sin -> sin_addr )) ;  
  164.           
  165.          ip_names [ nextAddr ] = ( char * ) malloc ( strlen ( temp ) + 1 ) ;  
  166.          if ( ip_names [ nextAddr ] == NULL )   
  167.          {   
  168.              return ;  
  169.          }   
  170.          strcpy ( ip_names [ nextAddr ] , temp ) ;  
  171.           
  172.          ip_addrs [ nextAddr ] = sin -> sin_addr . s_addr ;  
  173.           
  174.         ++ nextAddr ;  
  175.      }   
  176.       
  177.      close ( sockfd ) ;  
  178. }   
  179.    
  180. void GetHWAddresses ()   
  181. {   
  182.      struct ifconf ifc ;  
  183.      struct ifreq * ifr ;  
  184.      int i , sockfd ;  
  185.      char buffer [ BUFFERSIZE ] , * cp , * cplim ;  
  186.      char temp [ 80 ] ;  
  187.       
  188.      for ( i = 0 ; i < MAXADDRS ; ++ i )   
  189.      {   
  190.          hw_addrs [ i ] = NULL ;  
  191.      }   
  192.       
  193.      sockfd = socket ( AF_INET , SOCK_DGRAM , 0 ) ;  
  194.      if ( sockfd < 0 )   
  195.      {   
  196.          perror ( " socket failed " ) ;  
  197.          return ;  
  198.      }   
  199.       
  200.      ifc . ifc_len = BUFFERSIZE ;  
  201.      ifc . ifc_buf = buffer ;  
  202.       
  203.      if ( ioctl ( sockfd , SIOCGIFCONF , ( char * ) & ifc ) < 0 )   
  204.      {   
  205.          perror ( " ioctl error " ) ;  
  206.          close ( sockfd ) ;  
  207.          return ;  
  208.      }   
  209.       
  210.      ifr = ifc . ifc_req ;  
  211.       
  212.      cplim = buffer + ifc . ifc_len ;  
  213.       
  214.      for ( cp = buffer ; cp < cplim ; )   
  215.      {   
  216.          ifr = ( struct ifreq * ) cp ;  
  217.          if ( ifr -> ifr_addr . sa_family == AF_LINK )   
  218.          {   
  219.              struct sockaddr_dl * sdl = ( struct sockaddr_dl * ) & ifr -> ifr_addr ;  
  220.              int a , b , c , d , e , f ;  
  221.              int i ;  
  222.               
  223.              strcpy ( temp , ( char * ) ether_ntoa ( LLADDR ( sdl ))) ;  
  224.              sscanf ( temp , " %x:%x:%x:%x:%x:%x " , & a , & b , & c , & d , & e , & f ) ;  
  225.              sprintf ( temp , " %02X:%02X:%02X:%02X:%02X:%02X " , a , b , c , d , e , f ) ;  
  226.               
  227.              for ( i = 0 ; i < MAXADDRS ; ++ i )   
  228.              {   
  229.                  if (( if_names [ i ] != NULL ) && ( strcmp ( ifr -> ifr_name , if_names [ i ]) == 0 ))   
  230.                  {   
  231.                      if ( hw_addrs [ i ] == NULL )   
  232.                      {   
  233.                          hw_addrs [ i ] = ( char * ) malloc ( strlen ( temp ) + 1 ) ;  
  234.                          strcpy ( hw_addrs [ i ] , temp ) ;  
  235.                          break ;  
  236.                      }   
  237.                  }   
  238.              }   
  239.          }   
  240.          cp += sizeof ( ifr -> ifr_name ) + max ( sizeof ( ifr -> ifr_addr ) , ifr -> ifr_addr . sa_len ) ;  
  241.      }   
  242.       
  243.      close ( sockfd ) ;  
  244. }  
  245.   
  246.    
  247.   
  248.    
  249.   
  250. Test:  
  251.   
  252. #import " IPAdress.h "   
  253. ..........  
  254. - ( NSString * ) deviceIPAdress {   
  255.      InitAddresses () ;  
  256.      GetIPAddresses () ;  
  257.      GetHWAddresses () ;  
  258.      return [ NSString stringWithFormat :@ " %s " , ip_names [ 1 ]] ;  
  259. }   
  260.    
  261. - ( void ) viewDidLoad {   
  262.      [ super viewDidLoad ] ;  
  263.    
  264.      NSString * ip_iphone = [ self deviceIPAdress ] ;  
  265.    NSLog ( @ " ip:%@ " , ip_iphone ) ;  
  266. }  

Implicit declaration of function 'ether_ntoa' is invalid in C99

解决办法:http://stackoverflow.com/questions/11245280/implicit-declaration-of-function-ether-ntoa-is-invalid-in-c99

转自:http://blog.csdn.net/toddfox/article/details/5619890


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值