ios - 如何在 iOS 和 Mac OS X 上查找接口(interface)的硬件类型?

标签 ios macos cocoa cocoa-touch networking

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

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

为了避免不必要的请求并成为一名优秀的网络公民,我想明智地选择先尝试哪个界面。

我可以毫无问题地获取计算机上的接口(interface)列表,但这没有帮助: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

关于ios - 如何在 iOS 和 Mac OS X 上查找接口(interface)的硬件类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35705580/

相关文章:

objective-c - ARC 和 viewDidUnload

html - 黑色输入背景 - Cocoa WebView

ios - 在 iOS 上显示公司的 Twitter 提要

ios - 来自 IOS Xcode 的 Azure 推送通知错误

macos - 运行 go 给我 - go clang : error: no input files

objective-c - 在 WebView Cocoa 中放大和缩小

macos - 使 Root View Controller 在 mac OSX swift 中消失

objective-c - NSDrawer 委托(delegate)指向已释放的对象?

objective-c - 为什么 init 不是类方法?

ios - 如何创建一个包含 2 个 iOS 项目和一个 iOS 动态框架项目的 Xcode 6 工作区?