Created
November 12, 2014 11:21
-
-
Save priore/b91f6647c7fcca3b67cc to your computer and use it in GitHub Desktop.
Get list of all interfaces on the iPhone - iPad 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
| #include <sys/socket.h> | |
| #include <arpa/inet.h> | |
| #include <netdb.h> | |
| #include <sys/types.h> | |
| #include <ifaddrs.h> | |
| #include <net/if.h> | |
| - (NSArray *)localIPAddresses | |
| { | |
| NSMutableArray *ipAddresses = [NSMutableArray array] ; | |
| struct ifaddrs *allInterfaces; | |
| // Get list of all interfaces on the local machine: | |
| if (getifaddrs(&allInterfaces) == 0) { | |
| struct ifaddrs *interface; | |
| // For each interface ... | |
| for (interface = allInterfaces; interface != NULL; interface = interface->ifa_next) { | |
| unsigned int flags = interface->ifa_flags; | |
| struct sockaddr *addr = interface->ifa_addr; | |
| // Check for running IPv4, IPv6 interfaces. Skip the loopback interface. | |
| if ((flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING)) { | |
| if (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) { | |
| // Convert interface address to a human readable string: | |
| char host[NI_MAXHOST]; | |
| getnameinfo(addr, addr->sa_len, host, sizeof(host), NULL, 0, NI_NUMERICHOST); | |
| [ipAddresses addObject:[[NSString alloc] initWithUTF8String:host]]; | |
| } | |
| } | |
| } | |
| freeifaddrs(allInterfaces); | |
| } | |
| return ipAddresses; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment