如何找到iOS和Mac OS X上的界面的硬件类型? [英] How to find the hardware type of an interface on iOS and Mac OS X?

查看:343
本文介绍了如何找到iOS和Mac OS X上的界面的硬件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些代码,试探性地说明服务在网络接口上的可能性。我搜索的硬件不实现SSDP或mDNS,所以我必须手动寻找。



设备通过WiFi连接到网络,因此很有可能通过WiFi接口找到它。然而,有可能的Mac连接到WiFi网桥通过以太网,所以它可以很好地解决。



为了避免不必要的要求,并且一般来说是一个好的网络公民,我想知道哪个接口首先尝试。



我可以得到我的电脑上的接口列表没有问题,但是没有帮助: en0 在我的iMac,但我的Macbook上的WiFi。



奖金积分,如果这也适用于iOS,因为虽然很少,你可以使用USB以太网适配器。 / p>

解决方案

使用 SystemConfiguration 框架:



import Foundation
import SystemConfiguration

用于SCNetworkInterfaceCopyAll()中的接口,如NSArray {
如果let name = SCNetworkInterfaceGetBSDName (interface as!SCNetworkInterface),
let type = SCNetworkInterfaceGetInterfaceType(interface as!SCNetworkInterface){
print(Interface \(name)is type type \(type))
}
}



在我的系统上,输出:

 接口en0的类型为IEEE80211 
接口en3的类型为以太网
接口en1的类型为以太网
接口en2的类型以太网
接口bridge0的类型为Bridge


I'm writing some code that heuristically figures out the likelihood of a service being on a network interface. The hardware I'm searching for doesn't implement SSDP or mDNS, so I have to look for it manually.

The device connects to the network via WiFi, so it's most likely that I'll find it over the WiFi interface. However, it's possible for the Mac to be connected to a WiFi bridge via Ethernet, so it may well be resolvable through that.

To avoid unnecessary requests and to be generally a good network citizen, I'd like to be intelligent about which interface to try first.

I can get the list of interfaces on my computer no problem, but that's not helpful: en0 is wired ethernet on my iMac, but WiFi on my Macbook.

Bonus points if this works on iOS as well, since although it's rare you can use the USB Ethernet adapter with it.

解决方案

Use the SystemConfiguration framework:

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)")
    }
}

On my system, this prints:

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和Mac OS X上的界面的硬件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆