By Y.Will


2016-05-27 06:50:34 8 Comments

Since you know after June 1, app in iOS should support only-IPV6. I find a way that can change an ipv4 ip address to an ipv6 ip address. But, I cann't judge a network environment is only-IPV6. I already open a NAT64 WIFI by my MACBOOK, also use flight mode to make sure it's the only-IPV6. I just find there is an ipv6 ip address at first. Then work the code again, i will get both an ipv4 and an ipv6 address. I have no idea about it. Anyone have any idea?

There are my codes for fetch ip address:

- (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 comments

Related Questions

Sponsored Content

50 Answered Questions

[SOLVED] How to get client's IP address using JavaScript?

39 Answered Questions

[SOLVED] How to check for an active Internet connection on iOS or macOS?

23 Answered Questions

[SOLVED] How to get the client IP address in PHP?

33 Answered Questions

[SOLVED] How to change the name of an iOS app?

  • 2008-10-27 03:07:03
  • Robert Gould
  • 422349 View
  • 907 Score
  • 33 Answer
  • Tags:   ios

90 Answered Questions

[SOLVED] How can I make a UITextField move up when the keyboard is present?

45 Answered Questions

[SOLVED] How can I develop for iPhone using a Windows development machine?

  • 2008-08-22 13:35:01
  • ryan
  • 1074945 View
  • 1098 Score
  • 45 Answer
  • Tags:   ios windows macos

20 Answered Questions

[SOLVED] How do I check if a string contains another string in Objective-C?

25 Answered Questions

[SOLVED] How do I sort an NSMutableArray with custom objects in it?

6 Answered Questions

[SOLVED] Maximum length of the textual representation of an IPv6 address?

  • 2008-10-03 09:51:51
  • Gilles
  • 126814 View
  • 376 Score
  • 6 Answer
  • Tags:   ip ip-address ipv6

0 Answered Questions

NSXMLParser Leaks big time! Any suggestions?

Sponsored Content