Take the 2-minute tour ×

How would I make a GUI program that displays your Ip address with a click of a button? Please, no difficult explanations, I just started Cocoa not long ago.



share|improve this question
He wants to do this on a Mac, where he doesn't have VB. –  Cinder6 Aug 26 '09 at 2:45
Yea I've done the IP thing in Visual Basic, but I wanna try doing it in Cocoa –  lab12 Aug 26 '09 at 2:59
let me get that for you... youtube.com/watch?v=hkDD03yeLnU –  z8000 Aug 26 '09 at 3:45

4 Answers 4

For determining the IP address, I found this.

As for making it into a Cocoa app, add an NSTextField (label) to your main window in Interface Builder, put in a button, add in an application controller (a subclass of NSObject that you make), put in the outlet and the action, do the proper connenctions, and in the "get IP" method, put in that code and set the value for the label's stringValue.

You can use [[NSHost currentHost] address], but it won't always display what you like. On my system, for example, it gives my IPv6 address.

EDIT: On my system, [[[NSHost currentHost] addresses] objectAtIndex:0] has my IPv4 address.

share|improve this answer
for me it's not the first object, it's the last. Also, rather than using objectAtIndex, I suggest "lastObject": [[[NSHost currentHost] addresses] lastObject] –  auco Nov 11 '13 at 15:50

You can get IP address through two ways:

1- if you want to get the local ip address on the current used netwrok, you can use the following method to retrive it:

-(NSString *)getIPAddress
    NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0)
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL)
            if(temp_addr->ifa_addr->sa_family == AF_INET)
                    // Get NSString from C String
                    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
            temp_addr = temp_addr->ifa_next;

    // Free memory
    return address;

2- if you want to get the external IP address then you need to use the following method:

    NSUInteger  an_Integer;
    NSArray * ipItemsArray;
    NSString *externalIP;

    NSURL *iPURL = [NSURL URLWithString:@"http://www.dyndns.org/cgi-bin/check_ip.cgi"];

    if (iPURL) {
        NSError *error = nil;
        NSString *theIpHtml = [NSString stringWithContentsOfURL:iPURL encoding:NSUTF8StringEncoding error:&error];
        if (!error) {
            NSScanner *theScanner;
            NSString *text = nil;

            theScanner = [NSScanner scannerWithString:theIpHtml];

            while ([theScanner isAtEnd] == NO) {

                // find start of tag
                [theScanner scanUpToString:@"<" intoString:NULL] ;

                // find end of tag
                [theScanner scanUpToString:@">" intoString:&text] ;

                // replace the found tag with a space
                //(you can filter multi-spaces out later if you wish)
                theIpHtml = [theIpHtml stringByReplacingOccurrencesOfString:
                             [ NSString stringWithFormat:@"%@>", text]
                                                                 withString:@" "] ;
                ipItemsArray =[theIpHtml  componentsSeparatedByString:@" "];
                an_Integer=[ipItemsArray indexOfObject:@"Address:"];
                externalIP =[ipItemsArray objectAtIndex:  ++an_Integer];
        } else {
            NSLog(@"Oops... g %d, %@", [error code], [error localizedDescription]);
    return externalIP;
share|improve this answer
You should only call freeifaddrs() if getifaddrs() succeeded. –  Daniel Trebbien Jul 19 at 18:08

[[NSHost currentHost] addresses] will get you an array of IPs. Read the documentation for NSHost.

As for displaying that in a GUI, I recommend getting Aaron Hillegass' book Cocoa Programming for Mac OS X, or any Cocoa beginners book should teach that.

share|improve this answer
Is NSHost consistent in the ordering of the addresses? I've read some people saying it isn't, but that was talking about just plain address, not addresses. I've never had occasion to use NSHost. –  Cinder6 Aug 26 '09 at 2:19
Well, an array does maintain order, vs a dictionary.. I couldn't really say how reliable it is though. –  Phillip Jacobs Aug 26 '09 at 2:29
Alright. What I meant was, does it always go IPv6, IPv4, link local, IPv4 loopback, IPv6 loopback? –  Cinder6 Aug 26 '09 at 2:44
Hmm.. it seems like if you have IPv6 enabled, it'll show that first, then IPv4. Otherwise I think it follows the sequence of your system preferences -> network interfaces list. –  Phillip Jacobs Aug 26 '09 at 3:16
Incomplete answer. –  ComtriS Jun 25 '13 at 22:14

I just wrote this, may need some work but seems to work well on my machine...

- (NSString *)getLocalIPAddress
    NSArray *ipAddresses = [[NSHost currentHost] addresses];
    NSArray *sortedIPAddresses = [ipAddresses sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.allowsFloats = NO;

    for (NSString *potentialIPAddress in sortedIPAddresses)
        if ([potentialIPAddress isEqualToString:@""]) {

        NSArray *ipParts = [potentialIPAddress componentsSeparatedByString:@"."];

        BOOL isMatch = YES;

        for (NSString *ipPart in ipParts) {
            if (![numberFormatter numberFromString:ipPart]) {
                isMatch = NO;
        if (isMatch) {
            return potentialIPAddress;

    // No IP found
    return @"?.?.?.?";
share|improve this answer

Your Answer


