1

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.

Akbar
  • 1,509
  • 1
  • 16
  • 32
  • What does "first one is not the correct one" mean? What is the first one? What is the correct one? And why does it matter which is first? – David Schwartz Jul 20 '12 at 10:46
  • Its listing some 3 or more IP Addresses,In this list,the first one is the first IP Address which is some random IP Address,In System preferences,We can see the IP Address right,The IP Address in System preferences is the correct one. – Akbar Jul 20 '12 at 11:29
  • 1
    What is the actual first IP address? What makes you think it's "some random IP address" rather than the correct one? What makes the one you consider correct actually "correct"? What are your criteria? – David Schwartz Jul 20 '12 at 19:09

1 Answers1

2

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.

glglgl
  • 89,107
  • 13
  • 149
  • 217