Akbar
Akbar

Reputation: 1499

How to get the correct IPv6 address in cocoa

I have the following code snippet which is returning the correct IPV4 Address.

NSArray *addresses = [[NSHost currentHost] addresses];
NSString *stringAddress;

for (NSString *anAddress in addresses) {
    if (![anAddress hasPrefix:@"127"] && [[anAddress componentsSeparatedByString:@"."] count] == 4) {
        stringAddress = anAddress;
        NSLog(@"stringAddress is %@",stringAddress);
        break;
    } else {
   //     Do Something
    }
}

After running this application,these are the IPAddresses that NSHost address displaying.

2012-10-01 17:30:45.722 GetIPV6Address[1928:403] addresses is (
    "fe80::426c:8fff:fe07:f4e4%en0",
    "fdc7:2c68:e800::426c:8fff:fe07:f5e5",
    "fdc7:2c68:e800::fd62:f01c:155d:3228",                                                                                                  
    "fe80::7ec3:a1ff:fea5:c32d%en1",
    "198.168.10.144",)    

In my system preferences,I can see that my IPV4 address is 198.168.10.144 and IPV6 address is fdc7:2c68:e800::426c:8fff:fe07:f5e5.

This list is not always in the same sequence,so I am not able to get the right IP Address dynamically.From the list how could I find out that fdc7:2c68:e800::426c:8fff:fe07:f5e5 is my IPV6 address.Any help would be appreciated.

Upvotes: 1

Views: 829

Answers (1)

glglgl
glglgl

Reputation: 91017

You obviously have 2 interfaces, en0 and en1.

Both have a fe80: address (link local), and both have as well a fdc7:2c68:e800::... one, whatever this is. (site-local?)

They both should be reachable and usable.

Upvotes: 2

Related Questions