iOS 获取IP
- #import <ifaddrs.h> //获取IP
- #import <arpa/inet.h>
- //只能获取WIFI下的IP地址
- + (NSString *)getIPAddress {
- NSString *address = @"error";
- struct ifaddrs *interfaces = NULL;
- struct ifaddrs *temp_addr = NULL;
- int success = ;
- // retrieve the current interfaces - returns 0 on success
- success = getifaddrs(&interfaces);
- if (success == ) {
- // Loop through linked list of interfaces
- temp_addr = interfaces;
- while(temp_addr != NULL) {
- if(temp_addr->ifa_addr->sa_family == AF_INET) {
- // Check if interface is en0 which is the wifi connection on the iPhone
- if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
- // Get NSString from C String
- address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
- }
- }
- temp_addr = temp_addr->ifa_next;
- }
- }
- // Free memory
- freeifaddrs(interfaces);
- return address;
- }
- #import <ifaddrs.h> //获取IP
- #import <arpa/inet.h>
- #include <net/if.h>
- #define IOS_CELLULAR @"pdp_ip0"
- #define IOS_WIFI @"en0"
- //#define IOS_VPN @"utun0"
- #define IP_ADDR_IPv4 @"ipv4"
- #define IP_ADDR_IPv6 @"ipv6"
- //获取设备当前网络IP地址(包括4G 等)
- + (NSString *)getIPAddress:(BOOL)preferIPv4
- {
- NSArray *searchArray = preferIPv4 ?
- @[ /*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 ] :
- @[ /*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 ] ;
- NSDictionary *addresses = [self getIPAddresses];
- NSLog(@"addresses: %@", addresses);
- __block NSString *address;
- [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop)
- {
- address = addresses[key];
- if(address) *stop = YES;
- } ];
- return address ? address : @"0.0.0.0";
- }
- //获取所有相关IP信息
- + (NSDictionary *)getIPAddresses
- {
- NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:];
- // retrieve the current interfaces - returns 0 on success
- struct ifaddrs *interfaces;
- if(!getifaddrs(&interfaces)) {
- // Loop through linked list of interfaces
- struct ifaddrs *interface;
- for(interface=interfaces; interface; interface=interface->ifa_next) {
- if(!(interface->ifa_flags & IFF_UP) /* || (interface->ifa_flags & IFF_LOOPBACK) */ ) {
- continue; // deeply nested code harder to read
- }
- const struct sockaddr_in *addr = (const struct sockaddr_in*)interface->ifa_addr;
- char addrBuf[ MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN) ];
- if(addr && (addr->sin_family==AF_INET || addr->sin_family==AF_INET6)) {
- NSString *name = [NSString stringWithUTF8String:interface->ifa_name];
- NSString *type;
- if(addr->sin_family == AF_INET) {
- if(inet_ntop(AF_INET, &addr->sin_addr, addrBuf, INET_ADDRSTRLEN)) {
- type = IP_ADDR_IPv4;
- }
- } else {
- const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)interface->ifa_addr;
- if(inet_ntop(AF_INET6, &addr6->sin6_addr, addrBuf, INET6_ADDRSTRLEN)) {
- type = IP_ADDR_IPv6;
- }
- }
- if(type) {
- NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
- addresses[key] = [NSString stringWithUTF8String:addrBuf];
- }
- }
- }
- // Free memory
- freeifaddrs(interfaces);
- }
- return [addresses count] ? addresses : nil;
- }
iOS 获取IP的更多相关文章
- 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 ...
- iOS 获取设备信息,mac地址,IP地址,设备名称
#import "DeviceInfoUtil.h" #import "GlobleData.h" #import "sys/utsname.h&qu ...
- iOS 获取设备的ip地址
导入以下头文件 #include <ifaddrs.h> #include <arpa/inet.h> 通过下面方法即可获取ip地址+ (NSString *)getIpA ...
- iOS 获取设备型号以及IP地址
首先导入四个头文件 #include <sys/types.h> #include <sys/sysctl.h> #include <ifaddrs.h> #inc ...
- iOS根据域名获取ip地址
引入头文件 #include <netdb.h> #include <sys/socket.h> #include <arpa/inet.h> //根据域名获取ip ...
- iOS获取设备ip地址(OC版)
#import <SystemConfiguration/CaptiveNetwork.h> #import <ifaddrs.h> #import <arpa/inet ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- iOS获取设备唯一标识的8种方法
8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...
- 【PHP开发篇】一个统计客户端商机提交的获取IP地址
1.对客服提交数据的ip地址记录. 获取ip地址的方法: public function getIP() { global $ip; if (getenv("HTTP_X_REAL_IP&q ...
随机推荐
- Hadoop |集群的搭建
Hadoop组成 HDFS(Hadoop Distributed File System)架构概述 NameNode目录--主刀医生(nn): DataNode(dn)数据: Secondary N ...
- Shiro笔记(一)Shiro整体介绍
介绍:是一个java的安全(权限)框架 可以完成的功能:认证登录(Authentication).授权(Authorization).加密(cryptography).会话管理(session man ...
- 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity
异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...
- js根据银行卡号进行判断属于哪个银行并返回银行卡类型
在做绑定银行卡,输入银行卡的时候,产品有这么一个需求,需要用户输入银行卡号的时候,显示对应的银行卡名称及简称.于是苦苦寻觅,终于找到了支付宝的开放API,银行卡校验接口 https://ccdcapi ...
- POJ 1459 - Power Network 【Ek-最大流】
<题目链接> 题目大意:给出 n 个点,其中包括 np个发电站,nc 个消费者, 剩下的全部都是中转点,再给出 这些点中的m 条边,代表这两点间的最大传输电量,并且给出发电站的最大发送电量 ...
- SMB溢出漏洞所需的SMB协议内容
来一张wireshark抓下的SMB包,再解读一下. 一个包的内容有 网卡 IPV4 TCP NetBIOS SMB SMB header SMB Command: xxx Error class: ...
- 一个简单需求:HashMap实现相同key存入数据后不被覆盖
做一个积极的人 编码.改bug.提升自己 我有一个乐园,面向编程,春暖花开! 看似是一个简单的问题,其实里面包含很多的东西! 需求: 实现一个在HashMap中存入(任意类型)相同的key值后,key ...
- Codeforces Round #530 (Div. 2)
RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...
- Codeforces.618F.Double Knapsack(构造 鸽巢原理)
题目链接 \(Description\) 给定两个大小为\(n\)的可重集合\(A,B\),集合中的元素都在\([1,n]\)内.你需要从这两个集合中各选一个非空子集,使它们的和相等.输出方案. \( ...
- 潭州课堂25班:Ph201805201 django 项目 第三十九课 后台 文章发布,图片上传到 FastDFS后端实现 七牛云讲解(课堂笔记)
文章发布: # 1,从前台获取参数# 2,校验参数# 3,把数据保存到数据库# 4,返回执行结果到前台,(创建成功或失败) 自定义 froms.py 校验参数 上传图片到七牛云 注册 https:// ...