【问题标题】:How to find the hardware type of an interface on iOS and Mac OS X?如何在 iOS 和 Mac OS X 上查找接口的硬件类型?
【发布时间】:2016-06-12 20:21:06
【问题描述】:

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

设备通过 WiFi 连接到网络,所以我很可能会通过 WiFi 接口找到它。但是,Mac 可以通过以太网连接到 WiFi 桥接器,因此很可能可以通过它来解决。

为了避免不必要的请求并成为一名优秀的网络公民,我想知道先尝试哪个接口。

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

如果这在 iOS 上也可以使用,则可以加分,因为尽管很少有 USB 以太网适配器可以与它一起使用。

【问题讨论】:

  • 在命令行中看起来像networksetup -listallhardwareports 将为您的每个内置接口提供人类可读的类型。但是,它会错过任何通过 USB 连接的东西。 osxdaily.com/2014/09/03/…

标签: ios macos cocoa cocoa-touch networking


【解决方案1】:

使用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

【讨论】:

  • SCNetworkInterfaceCopyAll 在 iOS 上不可用 - 它仅适用于 macOS 10.4+。
【解决方案2】:

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
}

【讨论】:

    【解决方案3】:

    直接进入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) [网络接口显示];

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-08
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多