1. #import <ifaddrs.h> //获取IP
  2. #import <arpa/inet.h>
  3.  
  4. //只能获取WIFI下的IP地址
  5. + (NSString *)getIPAddress {
  6. NSString *address = @"error";
  7. struct ifaddrs *interfaces = NULL;
  8. struct ifaddrs *temp_addr = NULL;
  9. int success = ;
  10. // retrieve the current interfaces - returns 0 on success
  11. success = getifaddrs(&interfaces);
  12. if (success == ) {
  13. // Loop through linked list of interfaces
  14. temp_addr = interfaces;
  15. while(temp_addr != NULL) {
  16. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  17. // Check if interface is en0 which is the wifi connection on the iPhone
  18. if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
  19. // Get NSString from C String
  20. address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
  21. }
  22. }
  23. temp_addr = temp_addr->ifa_next;
  24. }
  25. }
  26. // Free memory
  27. freeifaddrs(interfaces);
  28. return address;
  29.  
  30. }
  31.  
  32. #import <ifaddrs.h> //获取IP
  33. #import <arpa/inet.h>
  34.  
  35. #include <net/if.h>
  36.  
  37. #define IOS_CELLULAR @"pdp_ip0"
  38. #define IOS_WIFI @"en0"
  39. //#define IOS_VPN @"utun0"
  40. #define IP_ADDR_IPv4 @"ipv4"
  41. #define IP_ADDR_IPv6 @"ipv6"
  42. //获取设备当前网络IP地址(包括4G 等)
  43. + (NSString *)getIPAddress:(BOOL)preferIPv4
  44. {
  45. NSArray *searchArray = preferIPv4 ?
  46. @[ /*IOS_VPN @"/" IP_ADDR_IPv4, IOS_VPN @"/" IP_ADDR_IPv6,*/ IOS_WIFI @"/" IP_ADDR_IPv4, IOS_WIFI @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6 ] :
  47. @[ /*IOS_VPN @"/" IP_ADDR_IPv6, IOS_VPN @"/" IP_ADDR_IPv4,*/ IOS_WIFI @"/" IP_ADDR_IPv6, IOS_WIFI @"/" IP_ADDR_IPv4, IOS_CELLULAR @"/" IP_ADDR_IPv6, IOS_CELLULAR @"/" IP_ADDR_IPv4 ] ;
  48.  
  49. NSDictionary *addresses = [self getIPAddresses];
  50. NSLog(@"addresses: %@", addresses);
  51.  
  52. __block NSString *address;
  53. [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
  54. {
  55. address = addresses[key];
  56. if(address) *stop = YES;
  57. } ];
  58. return address ? address : @"0.0.0.0";
  59. }
  60.  
  61. //获取所有相关IP信息
  62. + (NSDictionary *)getIPAddresses
  63. {
  64. NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:];
  65.  
  66. // retrieve the current interfaces - returns 0 on success
  67. struct ifaddrs *interfaces;
  68. if(!getifaddrs(&interfaces)) {
  69. // Loop through linked list of interfaces
  70. struct ifaddrs *interface;
  71. for(interface=interfaces; interface; interface=interface->ifa_next) {
  72. if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
  73. continue; // deeply nested code harder to read
  74. }
  75. const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
  76. char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
  77. if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
  78. NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
  79. NSString *type;
  80. if(addr->sin_family == AF_INET) {
  81. if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
  82. type = IP_ADDR_IPv4;
  83. }
  84. } else {
  85. const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
  86. if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
  87. type = IP_ADDR_IPv6;
  88. }
  89. }
  90. if(type) {
  91. NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
  92. addresses[key] = [NSString stringWithUTF8String:addrBuf];
  93. }
  94. }
  95. }
  96. // Free memory
  97. freeifaddrs(interfaces);
  98. }
  99. return [addresses count] ? addresses : nil;
  100. }

iOS 获取IP的更多相关文章

  1. iOS 获取IP地址

    一.获取本机IP地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #import <ifadd ...

  2. iOS 获取设备信息,mac地址,IP地址,设备名称

    #import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...

  3. iOS 获取设备的ip地址

    导入以下头文件 #include <ifaddrs.h> #include <arpa/inet.h>   通过下面方法即可获取ip地址+ (NSString *)getIpA ...

  4. iOS 获取设备型号以及IP地址

    首先导入四个头文件 #include <sys/types.h> #include <sys/sysctl.h> #include <ifaddrs.h> #inc ...

  5. iOS根据域名获取ip地址

    引入头文件 #include <netdb.h> #include <sys/socket.h> #include <arpa/inet.h> //根据域名获取ip ...

  6. iOS获取设备ip地址(OC版)

    #import <SystemConfiguration/CaptiveNetwork.h> #import <ifaddrs.h> #import <arpa/inet ...

  7. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  8. iOS获取设备唯一标识的8种方法

    8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...

  9. 【PHP开发篇】一个统计客户端商机提交的获取IP地址

    1.对客服提交数据的ip地址记录. 获取ip地址的方法: public function getIP() { global $ip; if (getenv("HTTP_X_REAL_IP&q ...

随机推荐

  1. Hadoop |集群的搭建

    Hadoop组成 HDFS(Hadoop Distributed File System)架构概述 NameNode目录--主刀医生(nn):  DataNode(dn)数据: Secondary N ...

  2. Shiro笔记(一)Shiro整体介绍

    介绍:是一个java的安全(权限)框架 可以完成的功能:认证登录(Authentication).授权(Authorization).加密(cryptography).会话管理(session man ...

  3. 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity

    异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...

  4. js根据银行卡号进行判断属于哪个银行并返回银行卡类型

    在做绑定银行卡,输入银行卡的时候,产品有这么一个需求,需要用户输入银行卡号的时候,显示对应的银行卡名称及简称.于是苦苦寻觅,终于找到了支付宝的开放API,银行卡校验接口 https://ccdcapi ...

  5. POJ 1459 - Power Network 【Ek-最大流】

    <题目链接> 题目大意:给出 n 个点,其中包括 np个发电站,nc 个消费者, 剩下的全部都是中转点,再给出 这些点中的m 条边,代表这两点间的最大传输电量,并且给出发电站的最大发送电量 ...

  6. SMB溢出漏洞所需的SMB协议内容

    来一张wireshark抓下的SMB包,再解读一下. 一个包的内容有 网卡 IPV4 TCP NetBIOS SMB SMB header SMB Command: xxx Error class: ...

  7. 一个简单需求:HashMap实现相同key存入数据后不被覆盖

    做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 看似是一个简单的问题,其实里面包含很多的东西! 需求: 实现一个在HashMap中存入(任意类型)相同的key值后,key ...

  8. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  9. Codeforces.618F.Double Knapsack(构造 鸽巢原理)

    题目链接 \(Description\) 给定两个大小为\(n\)的可重集合\(A,B\),集合中的元素都在\([1,n]\)内.你需要从这两个集合中各选一个非空子集,使它们的和相等.输出方案. \( ...

  10. 潭州课堂25班:Ph201805201 django 项目 第三十九课 后台 文章发布,图片上传到 FastDFS后端实现 七牛云讲解(课堂笔记)

    文章发布: # 1,从前台获取参数# 2,校验参数# 3,把数据保存到数据库# 4,返回执行结果到前台,(创建成功或失败) 自定义 froms.py 校验参数 上传图片到七牛云 注册 https:// ...