Created
March 30, 2013 06:44
-
-
Save wanderwaltz/5275661 to your computer and use it in GitHub Desktop.
Get the IP address of an iOS device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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