歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 獲取iOS設備當前ip地址

獲取iOS設備當前ip地址

日期:2017/3/1 11:11:19   编辑:Linux編程

第一種方法是用系統api的方式獲取,如下

  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. //#include "GetAddresses.h"
  16. #define min(a,b) ((a) < (b) ? (a) : (b))
  17. #define max(a,b) ((a) > (b) ? (a) : (b))
  18. #define BUFFERSIZE 4000
  19. char *if_names[MAXADDRS];
  20. char *ip_names[MAXADDRS];
  21. char *hw_addrs[MAXADDRS];
  22. unsigned long ip_addrs[MAXADDRS];
  23. static int nextAddr = 0;
  24. void InitAddresses()
  25. {
  26. int i;
  27. for (i=0; i<MAXADDRS; ++i)
  28. {
  29. if_names[i] = ip_names[i] = hw_addrs[i] = NULL;
  30. ip_addrs[i] = 0;
  31. }
  32. }
  33. void FreeAddresses()
  34. {
  35. int i;
  36. for (i=0; i<MAXADDRS; ++i)
  37. {
  38. if (if_names[i] != 0) free(if_names[i]);
  39. if (ip_names[i] != 0) free(ip_names[i]);
  40. if (hw_addrs[i] != 0) free(hw_addrs[i]);
  41. ip_addrs[i] = 0;
  42. }
  43. InitAddresses();
  44. }
  45. void GetIPAddresses()
  46. {
  47. int i, len, flags;
  48. char buffer[BUFFERSIZE], *ptr, lastname[IFNAMSIZ], *cptr;
  49. struct ifconf ifc;
  50. struct ifreq *ifr, ifrcopy;
  51. struct sockaddr_in *sin;
  52. char temp[80];
  53. int sockfd;
  54. for (i=0; i<MAXADDRS; ++i)
  55. {
  56. if_names[i] = ip_names[i] = NULL;
  57. ip_addrs[i] = 0;
  58. }
  59. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  60. if (sockfd < 0)
  61. {
  62. perror("socket failed");
  63. return;
  64. }
  65. ifc.ifc_len = BUFFERSIZE;
  66. ifc.ifc_buf = buffer;
  67. if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
  68. {
  69. perror("ioctl error");
  70. return;
  71. }
  72. lastname[0] = 0;
  73. for (ptr = buffer; ptr < buffer + ifc.ifc_len; )
  74. {
  75. ifr = (struct ifreq *)ptr;
  76. len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
  77. ptr += sizeof(ifr->ifr_name) + len; // for next one in buffer
  78. if (ifr->ifr_addr.sa_family != AF_INET)
  79. {
  80. continue; // ignore if not desired address family
  81. }
  82. if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL)
  83. {
  84. *cptr = 0; // replace colon will null
  85. }
  86. if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0)
  87. {
  88. continue; /* already processed this interface */
  89. }
  90. memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
  91. ifrcopy = *ifr;
  92. ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
  93. flags = ifrcopy.ifr_flags;
  94. if ((flags & IFF_UP) == 0)
  95. {
  96. continue; // ignore if interface not up
  97. }
  98. if_names[nextAddr] = (char *)malloc(strlen(ifr->ifr_name)+1);
  99. if (if_names[nextAddr] == NULL)
  100. {
  101. return;
  102. }
  103. strcpy(if_names[nextAddr], ifr->ifr_name);
  104. sin = (struct sockaddr_in *)&ifr->ifr_addr;
  105. strcpy(temp, inet_ntoa(sin->sin_addr));
  106. ip_names[nextAddr] = (char *)malloc(strlen(temp)+1);
  107. if (ip_names[nextAddr] == NULL)
  108. {
  109. return;
  110. }
  111. strcpy(ip_names[nextAddr], temp);
  112. ip_addrs[nextAddr] = sin->sin_addr.s_addr;
  113. ++nextAddr;
  114. }
  115. close(sockfd);
  116. }
  117. void GetHWAddresses()
  118. {
  119. struct ifconf ifc;
  120. struct ifreq *ifr;
  121. int i, sockfd;
  122. char buffer[BUFFERSIZE], *cp, *cplim;
  123. char temp[80];
  124. for (i=0; i<MAXADDRS; ++i)
  125. {
  126. hw_addrs[i] = NULL;
  127. }
  128. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  129. if (sockfd < 0)
  130. {
  131. perror("socket failed");
  132. return;
  133. }
  134. ifc.ifc_len = BUFFERSIZE;
  135. ifc.ifc_buf = buffer;
  136. if (ioctl(sockfd, SIOCGIFCONF, (char *)&ifc) < 0)
  137. {
  138. perror("ioctl error");
  139. close(sockfd);
  140. return;
  141. }
  142. ifr = ifc.ifc_req;
  143. cplim = buffer + ifc.ifc_len;
  144. for (cp=buffer; cp < cplim; )
  145. {
  146. ifr = (struct ifreq *)cp;
  147. if (ifr->ifr_addr.sa_family == AF_LINK)
  148. {
  149. struct sockaddr_dl *sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
  150. int a,b,c,d,e,f;
  151. int i;
  152. strcpy(temp, (char *)ether_ntoa(LLADDR(sdl)));
  153. sscanf(temp, "%x:%x:%x:%x:%x:%x", &a, &b, &c, &d, &e, &f);
  154. sprintf(temp, "%02X:%02X:%02X:%02X:%02X:%02X",a,b,c,d,e,f);
  155. for (i=0; i<MAXADDRS; ++i)
  156. {
  157. if ((if_names[i] != NULL) && (strcmp(ifr->ifr_name, if_names[i]) == 0))
  158. {
  159. if (hw_addrs[i] == NULL)
  160. {
  161. hw_addrs[i] = (char *)malloc(strlen(temp)+1);
  162. strcpy(hw_addrs[i], temp);
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. cp += sizeof(ifr->ifr_name) + max(sizeof(ifr->ifr_addr), ifr->ifr_addr.sa_len);
  169. }
  170. close(sockfd);
  171. }
  1. - (NSString *)deviceIPAdress {
  2. InitAddresses();
  3. GetIPAddresses();
  4. GetHWAddresses();
  5. return [NSString stringWithFormat:@"%s", ip_names[1]];
  6. }
這樣得到的ip,如果在內網中,那麼就是內網的ip.

第二種方法是可以獲取公網ip

  1. - (void)getCurrentIP
  2. {
  3. NSURL *url = [NSURL URLWithString:@"http://automation.whatismyip.com/n09230945.asp"];
  4. __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  5. [request setCompletionBlock:^{
  6. NSString *responseString = [request responseString];
  7. if (responseString) {
  8. NSString *ip = [NSString stringWithFormat:@"%@", responseString];
  9. NSLog(@"responseString = %@", ip);
  10. };
  11. }];
  12. [request setFailedBlock:^{
  13. }];
  14. }
用到了ASIHTTPRequest這個網絡開源庫。 獲取你當前的ip地址。
Copyright © Linux教程網 All Rights Reserved