How can I detect if any network adapter is connected? I can only find examples on using NSReachability to detect an internet connection, but I want to detect even a non-internet network connection. Getting the IP-adress on eth0 should work? I'm working on Mac only.
-
2Are you working on iOS, Mac, or both? – Bryan Oct 08 '12 at 19:57
-
OK, I've only tried this on iPhone, so my answer might not work on Mac. I'll see if I can do some more research. – Bryan Oct 09 '12 at 06:50
-
That would be greatly appreciated. – Andreas Bergström Oct 09 '12 at 06:58
-
You should not rely on particular interface names such as "eth0". On an iMac, "en0" can be the (unconnected) Ethernet interface and "en1" the (connected) WiFi interface. A Mac Pro can have 2 Ethernet interfaces "en0" and "en1". – Martin R Oct 15 '12 at 07:03
3 Answers
Getting a List of All IP Addresses in Apple's Technical Note TN1145 mentions 3 methods for getting the status of the network interfaces:
- System Configuration Framework
- Open Transport API
- BSD sockets
System Configuration Framework: This is Apple's recommended way and there is sample code in the TN1145. The advantage is that it provides a way to get notified of changes in the interface configuration.
Open Transport API: There is also sample code in TN1145, otherwise I cannot say much about it. (There is only "legacy" documentation on the Apple website.)
BSD sockets: This seems to be the easiest way to get the list of interfaces and to determine the connection status (if you don't need dynamic change notifications).
The following code demonstrates how to find all IPv4 and IPv6 interfaces that are "up and running".
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netdb.h>
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);
printf("interface:%s, address:%s\n", interface->ifa_name, host);
}
}
}
freeifaddrs(allInterfaces);
}

- 529,903
- 94
- 1,240
- 1,382
-
Thanks a lot, best answer so far. I don't have time to try it right now but unless something better drops in the coming 6 hours I will accept it. – Andreas Bergström Oct 15 '12 at 13:37
-
You can use the Reachability code, provided by apple. Here's the link where you could get the "Reachability" source code:
You can also download this file: TestWifi in Github. It will show you how to implement the Reachability class.
Hope this could help you.

- 67
- 6
Reachability
(I assume you meant Apple's demo class based on the underlying SCNetworkReachability...
API) works for any IP-connected host, including on your local network. You could use the reachabilityForLocalWiFi
method, although according to this page it will return YES when the network is active but not routable. So you may prefer to query reachabilityWithAddress:
with a local address.
This is a drop-in replacement for Reachability
that some people recommend.

- 11,398
- 3
- 53
- 78