如何在iOS和Mac OS X上找到接口的硬件类型?

我正在编写一些代码,启发式地计算出服务在网络接口上的可能性。 我正在搜索的硬件没有实现SSDP或mDNS,所以我必须手动查找。

该设备通过WiFi连接到网络,因此我很可能通过WiFi接口找到它。 但是,Mac可以通过以太网连接到WiFi网桥,因此可以通过它进行解析。

为了避免不必要的请求并且通常是一个良好的网络公民,我想知道首先尝试哪个界面。

我可以在我的计算机上获得接口列表没问题,但这没有用: en0是我的iMac上的有线以太网,但我的Macbook上是WiFi。

奖励积分如果这也适用于iOS,因为虽然很少有人可以使用USB以太网适配器。

使用SystemConfiguration框架:

 import Foundation import SystemConfiguration for interface in SCNetworkInterfaceCopyAll() as NSArray { if let name = SCNetworkInterfaceGetBSDName(interface as! SCNetworkInterface), let type = SCNetworkInterfaceGetInterfaceType(interface as! SCNetworkInterface) { print("Interface \(name) is of type \(type)") } } 

在我的系统上,这打印:

 Interface en0 is of type IEEE80211 Interface en3 is of type Ethernet Interface en1 is of type Ethernet Interface en2 is of type Ethernet Interface bridge0 is of type Bridge 

不是很多Mac开发人员,但在iOS我们可以使用Apple提供的Reachability类。

 Reachability *reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifier]; NetworkStatus status = [reachability currentReachabilityStatus]; if(status == NotReachable) { //No Connection } else if (status == ReachableViaWiFi) { //WiFi Connection } else if (status == ReachableViaWWAN) { //Carrier Connection } 

直接进入C :(作为奖励获得IP)

 @implementation NetworkInterfaces +(void)display{ struct ifaddrs *ifap, *ifa; struct sockaddr_in *sa; char *addr; getifaddrs (&ifap); for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family==AF_INET) { sa = (struct sockaddr_in *) ifa->ifa_addr; addr = inet_ntoa(sa->sin_addr); printf("Interface: %s\tAddress: %s\n", ifa->ifa_name, addr); } } freeifaddrs(ifap); } @end 

在控制器(或AppDelegate)中:

(迅速)

 NetworkInterfaces.display() 

(objC)[NetworkInterfaces显示];