3

I'm programming in objective-C for several iPod devices and I was wondering about something. I'm developing an application that utilizes the server-client model and I'm using the UDP protocol with C sockets. Is there a class out there that allows me to determine the iPod devices IP address? After googling around other forums, I haven't found anything. Obviously this command wouldn't work, but something like ipAddress = self.ip is what I had in mind. I'm setting up multicast C sockets and I'm trying to do a workaround that resembles the ping command, which obviously doesn't exist in objective-C either or to my knowledge (which is limited, as I've only been programming in objective-C since the start of this summer) at least. Any advice or tips?

Kara
  • 6,115
  • 16
  • 50
  • 57
Josh Bradley
  • 4,630
  • 13
  • 54
  • 79

2 Answers2

8

This snippet of code will retrieve it by looping through the interfaces.

- (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)
            {
                // Check if interface is en0 which is the wifi connection on the iPhone  
                if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])  
                {
                    // 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
    freeifaddrs(interfaces); 
    return address; 
} 
dreadwail
  • 15,098
  • 21
  • 65
  • 96
  • First test and run the code , then post the full code. i am getting warings and error from the above code. warning: implicit declaration of function 'getifadrs' error: dereferencing pointter to incomplete type for (temp_addr != NULL) and few more errors. – Biranchi Oct 30 '09 at 07:45
  • 6
    You didn't include the proper headers. First understand the code before copying and pasting it off the bathroom wall. – dreadwail Feb 11 '11 at 21:17
  • Anyone want to give us some incite on what the proper headers are? – AddisDev Aug 02 '12 at 15:50
  • 4
    @TaylorAddison `#import #import #import ` @BenLakey Thanks for your solution – clauswey Nov 13 '12 at 10:17
0

Did you see this? http://www.appsamuck.com/day4.html. I think the right answer is to use CFHost in the SDK.

EDIT
It appears the source in that project is using the following code, which makes it a completely invalid solution unless Apple decides to put NSHost into the SDK.

-(NSString*)getAddress {  
    char iphone_ip[255];  
    strcpy(iphone_ip,"127.0.0.1"); // if everything fails  
    NSHost* myhost =[NSHost currentHost];  
    if (myhost)  
    {  
        NSString *ad = [myhost address];  
        if (ad)  
            strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);  
    }  
    return [NSString stringWithFormat:@"%s",iphone_ip];   
}
marcc
  • 12,295
  • 7
  • 49
  • 59