如何在iOS中判断网络环境是Only-IPV6

时间:2016-05-27 06:50:35

标签: ios ip-address ipv6

因为你知道6月1日之后,iOS中的app应该只支持-IPV6。我找到了一种可以将ipv4 ip地址改为ipv6 ip地址的方法。但是,我无法判断网络环境只有-IPV6。我已经通过我的MACBOOK打开了NAT64 WIFI,也使用了飞行模式来确保它是唯一的IPV6。我刚开始发现有一个ipv6的ip地址。然后再次使用代码,我将获得ipv4和ipv6地址。我不知道。有人有任何想法吗?

有我的获取IP地址的代码:

- (NSMutableDictionary *)localIPAddressFetcher {
    NSMutableDictionary *addressDic = [NSMutableDictionary dictionary];
    struct ifaddrs *myaddrs, *temp_addr;
    struct sockaddr_in *s4;
    struct sockaddr_in6 *s6;
    int status = 0;
    char buf[64];
    status = getifaddrs(&myaddrs);
    if (status == 0) {
        for (temp_addr = myaddrs; temp_addr != NULL; temp_addr = temp_addr->ifa_next) {
            if (temp_addr->ifa_addr == NULL) continue;
            if ((temp_addr->ifa_flags & IFF_UP) == 0) continue;
            if (temp_addr->ifa_flags & IFF_LOOPBACK) continue;
            if (temp_addr->ifa_addr->sa_family == AF_INET) {
                s4 = (struct sockaddr_in *)(temp_addr->ifa_addr);
                if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s4->sin_addr), buf, sizeof(buf)) != NULL) {
                    if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"lo0"]) {
                        NSString *address = [NSString stringWithUTF8String:buf];
                        if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                            [addressDic setObject:address forKey:@"ipv4_wifi"];
                        } else {
                            [addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
                        }
                    }
                }
            } else if (temp_addr->ifa_addr->sa_family == AF_INET6) {
                s6 = (struct sockaddr_in6 *)(temp_addr->ifa_addr);
                if (inet_ntop(temp_addr->ifa_addr->sa_family, (void *)&(s6->sin6_addr), buf, sizeof(buf)) != NULL) {
                    if (![[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"lo0"]) {
                        NSString *address = [NSString stringWithUTF8String:buf];
                        if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
                            [addressDic setObject:address forKey:@"ipv6_wifi"];
                        } else {
                            [addressDic setObject:address forKey:[NSString stringWithUTF8String:temp_addr->ifa_name]];
                        }
                    }
                }
            }
        }
    }
    freeifaddrs(myaddrs);

return addressDic;}

0 个答案:

没有答案