转自:
可能问题太简单了,在网上没有找到答案,只好我自己写了。
由于苹果审核我们的应用在ipv6环境下,无法连接到服务器。所以把原来的用IP作为请求网址改成了域名来发请求。虽然审核过了,但是在国内还有域名解析的问题,使用域名有可能没有解析到ip地址,有可能解析的ip地址不正确(可能域名转发引起的,域名转发已经被国家禁止)。
所以我想如果手机当前连接的ipv4的网络,还用原来的ip请求。如果手机连接的是ipv6网络,则使用域名连接。
下面我对不同的设备分别使用ipv4(wifi,4G)和ipv6(wifi)网络进行测试:
| IPV4 | IPV6 |
IPAD(WIFI) | en0/ipv4 = 192.168.2.90, lo0/ipv6 = fe80::1, awdl0/ipv6 = fe80::5cbf:1aff:fe4e:ef38, en0/ipv6 = fe80::ae:8811:f45b:198a, lo0/ipv4 = 127.0.0.1 | en0/ipv4 = 169.254.229.160, lo0/ipv6 = fe80::1, awdl0/ipv6 = fe80::5cbf:1aff:fe4e:ef38, en0/ipv6 = 2001:2::aab1:6005:dcd1:6ffd:63e4, lo0/ipv4 = 127.0.0.1 |
IPHONE6(WIFI) | en0/ipv4 = 192.168.8.110, lo0/ipv6 = fe80::1, awdl0/ipv6 = fe80::b84d:41ff:fe6e:2925, en0/ipv6 = fe80::435:13b1:816c:9db9, lo0/ipv4 = 127.0.0.1 | en0/ipv4 = 169.254.87.24, lo0/ipv6 = fe80::1, awdl0/ipv6 = fe80::b84d:41ff:fe6e:2925, en0/ipv6 = 2001:2::aab1:b19e:527a:45d3:9f, lo0/ipv4 = 127.0.0.1 |
IPHONE6S(WIFI) | en0/ipv4 = 192.168.2.94, lo0/ipv6 = fe80::1, awdl0/ipv6 = fe80::a0ff:ddff:fec0:a676, en0/ipv6 = fe80::444:d26c:1b78:8f89, lo0/ipv4 = 127.0.0.1, utun0/ipv6 = fe80::3213:2aa4:9fed:9339 | |
IPHONE6PLUS (4G) | lo0/ipv6 = fe80::1, pdp_ip0/ipv4 = 10.241.179.135, lo0/ipv4 = 127.0.0.1, utun0/ipv6 = fe80::e7dd:8eef:da5e:f8f7 | |
IPHONE4(WIFI) | lo0/ipv4 = 127.0.0.1, en0/ipv4 = 192.168.2.138, lo0/ipv6 = fe80::1, en0/ipv6 = fe80::1420:27e5:d334:e8de | lo0/ipv4 = 127.0.0.1, en0/ipv4 = 169.254.120.241, lo0/ipv6 = fe80::1, en0/ipv6 = 2001:2::aab1:509d:9f42:38e0:a945 |
| | |
| | |
经过分析发现:
1.lo0/ipv6 = fe80::1,这个是固定的
2.lo0/ipv4 = 127.0.0.1 这个也是固定的
3.ipv6下 en0/ipv4 = 169.254.120.241,是固定的
4.ipv4下:awdl0/ipv6,en0/ipv6,utun0/ipv6对应的值都是fe80 开头
结论:在ipv6下,awdl0/ipv6,en0/ipv6,utun0/ipv6对应的值肯定有一个不是以fe80 开头
还有好多情况没有考虑eg:VPN
下面开始写代码来判断:
导入头文件和宏定义
- #define IOS_CELLULAR @"pdp_ip0"
- #define IOS_WIFI @"en0"
- #define IOS_VPN @"utun0"
- #define IP_ADDR_IPv4 @"ipv4"
- #define IP_ADDR_IPv6 @"ipv6"
- #import <ifaddrs.h>
- #import <arpa/inet.h>
- #import <net/if.h>
添加两个方法
- + (BOOL)isIpv6{
- NSArray *searchArray =
- @[ 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 BOOL isIpv6 = NO;
- [searchArray enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOLBOOL *stop)
- {
-
- NSLog(@"---%@---%@---",key, addresses[key] );
-
- if ([key rangeOfString:@"ipv6"].length > 0 && ![[NSString stringWithFormat:@"%@",addresses[key]] hasPrefix:@"(null)"] ) {
-
- if ( ![addresses[key] hasPrefix:@"fe80"]) {
- isIpv6 = YES;
- }
- }
-
- } ];
-
- return isIpv6;
- }
-
-
- + (NSDictionary *)getIPAddresses
- {
- NSMutableDictionary *addresses = [NSMutableDictionary dictionaryWithCapacity:8];
-
- struct ifaddrs *interfaces;
- if(!getifaddrs(&interfaces)) {
-
- struct ifaddrs *interface;
- for(interface=interfaces; interface; interface=interface->ifa_next) {
- if(!(interface->ifa_flags & IFF_UP) ) {
- continue;
- }
- 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;
-
- NSLog(@"ipv4 %@",name);
- }
- } 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;
- NSLog(@"ipv6 %@",name);
-
- }
- }
- if(type) {
- NSString *key = [NSString stringWithFormat:@"%@/%@", name, type];
- addresses[key] = [NSString stringWithUTF8String:addrBuf];
- }
- }
- }
-
- freeifaddrs(interfaces);
- }
- return [addresses count] ? addresses : nil;
- }
调用
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
-
- NSLog(@"###-###%d",[ViewController isIpv6]); ;
-
- if ([ViewController isIpv6] == YES) {
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是ipv6" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alertView show];
-
- }else{
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不是ipv6" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];
- [alertView show];
- }
-
-
-
- }
代码已上传
http://download.csdn.net/detail/qq_15509071/9743137