Created
November 14, 2013 16:25
-
-
Save jacob414/7469727 to your computer and use it in GitHub Desktop.
Get IP address of iOS device
This file contains hidden or 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
#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