Skip to content

Instantly share code, notes, and snippets.

@wanderwaltz
Created March 30, 2013 06:44
Show Gist options
  • Save wanderwaltz/5275661 to your computer and use it in GitHub Desktop.
Save wanderwaltz/5275661 to your computer and use it in GitHub Desktop.
Get the IP address of an iOS device.
- (NSString *) getIPAddress
{
NSString *address = nil;
structifaddrs *interfaces = NULL;
structifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0)
{
temp_addr = interfaces;
while (temp_addr != NULL)
{
if (temp_addr->ifa_addr->sa_family == AF_INET)
{
if ([[NSStringstringWithUTF8String: temp_addr->ifa_name] isEqualToString: @"en0"])
{
address = [NSStringstringWithUTF8String:
inet_ntoa(((structsockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment