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?
Asked
Active
Viewed 5,887 times
3
2 Answers
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
-
6You 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 – clauswey Nov 13 '12 at 10:17#import ` @BenLakey Thanks for your solution
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
-
-
NSHost is not supported by the iPhone SDK. It would be great if it were though. – Josh Bradley Jul 10 '09 at 20:32
-
-
1No, it is not. See Apple’s Technical Q&A QA1652 for proof. NSHost is private. – yakovlev Mar 14 '10 at 16:47