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 ...
随机推荐
- OpenJ_Bailian 2814 拨钟问题
总时间限制: 1000ms 内存限制: 65536kB 描述 有9个时钟,排成一个3*3的矩阵. |-------| |-------| |-------|| | | | | | ||---O | ...
- ESP8266进阶篇
ESP8266进阶篇 20170225(应需要,继续使用此模块!!!) 说一下如何通过内网和外网来控制我的ESP8266的数据模块 1.内网控制:(要求手机直接连接在ESP8266的WIFI上面,使用 ...
- tp5的路由
路由模式:普通.强制和混合 普通模式: //配置文件关闭路由,完全使用默认的PATH_INFO方式URL 'url_route_on' => false, 关闭路由后的普通模式任然可以通过操作方 ...
- Peter's smokes -poj 2509
题意:彼得有n支雪茄,每k个烟头可以换一支新雪茄,问彼得最多可以吸多少支雪茄 ? 当时自己做时,错在了直接在while循环开始前,便将雪茄的初始数量给加上了,然而应该是先处理后再加上最终剩余的雪茄数量 ...
- Selenium+PhantomJS使用时报错原因及解决方案(转)
Selenium+PhantomJS使用时报错原因及解决方案 问题 今天在使用selenium+PhantomJS动态抓取网页时,出现如下报错信息: UserWarning: Selenium ...
- 牛客练习赛37-筱玛的字符串-DP递推
筱玛的字符串 思路 :dp [ i ] [ j ] [ 3 ] 分别代表到第 i 位时 左括号比右括号多 j ,后面有三个状态 分别表示当前位置 S3的字符 是正在反转的,还是 反转完成的,还是没有反 ...
- SpringBoot使用WebJars
本人主要做的是java,但是从第一份工作开始,就一直在做一个写前端又写后端的程序员,相信很多朋友和我一样,不仅要会后台代码,还要懂得很多的前端代码,例如javascipt和css样式. 本文就为大家简 ...
- vuex那些事儿
vuex适用于大型单页应用.每一个Vuex应用的核心就是store(仓库),store中储存大量状态,Vuex的状态存储是响应式的.vuex使用单一状态树,一个对象包含了全部的应用层级状态,每个应用仅 ...
- Lucene入门实例-CRUD
1.导入jar包 lucene-analyzers-common-7.6.0.jar lucene-analyzers-smartcn-7.6.0.jar lucene-core-7.6.0.jar ...
- 机器学习系列-tensorflow-01-急切执行API
tensorflow急切执行概述 Eager execution is an imperative, define-by-run interface where operations are exec ...