Skip to content

Instantly share code, notes, and snippets.

@jacob414
Created November 14, 2013 16:25
Show Gist options
  • Save jacob414/7469727 to your computer and use it in GitHub Desktop.
Save jacob414/7469727 to your computer and use it in GitHub Desktop.
Get IP address of iOS device
#import <ifaddrs.h>
#import <arpa/inet.h>
/**
* Returns the IP adress of the device.
*/
- (NSString*)ipaddr {
NSString *address = nil;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *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(strcmp(temp_addr->ifa_name, "en0") == 0) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment