ios 是否可以在应用程序中获取当前连接的 WiFi 网络的 SSID 和 MAC 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16470615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to get the SSID & MAC Address of Currently connected WiFi Network in an App
提问by aDam_Nerd
I am looking for a way to get both the MAC Adress and the SSID of the currently connected WiFi Network in my project. I have used Tony Million's Reachabilityto decide when the user is on a WiFi network or not and was testing Kenial's NICInfoonly to find out that it only provides the iPhone's WiFi MAC Address. Although both projects helped greatly in there own way, they do not get the job done. I am wondering if there is a public API (for certain Apple App Approval) or some back door to achieve this.
我正在寻找一种方法来获取我的项目中当前连接的 WiFi 网络的 MAC 地址和 SSID。我使用Tony Million 的 Reachability来决定用户何时在 WiFi 网络上,并且测试Kenial 的 NICInfo只是为了发现它只提供 iPhone 的 WiFi MAC 地址。尽管这两个项目都以自己的方式提供了很大帮助,但它们并没有完成工作。我想知道是否有公共 API(对于某些 Apple App Approval)或一些后门来实现这一点。
回答by Jai Govindani
This involves a few different things:
这涉及到一些不同的事情:
Getting SSID - This is independent of the MAC address issue. For this, check this SO answer: iPhone get SSID without private library. I don't know what the etiquette is regarding re-posting code from other answers so I'll just link.
Getting MAC address - since the majority of networking operates at Layer 3 (which is called, who would have guessed, the Network Layer - http://en.wikipedia.org/wiki/OSI_model) and the MAC address is at Layer 2 (the Data Link layer) you'll first need to get the IP address of the gateway of the WiFi interface. Just a side note - the question shouldbe phrased as 'How do I get the SSID of the currently connected WiFi network and the MAC address of the WiFi router/gateway'. Anywho, to get the gateway address, see this SO answer: How to get the WIFI gateway address on the iPhone?
Once you have that, you need to convert it to a MAC address using ARP (Address Resolution Protocol, which is the protocol that all networks use to convert IP addresses to MAC addresses). Network interfaces don't communicate via IP, they communicate via MAC, so this actually happens everywhere, all the time. It's actually pretty easy to access the ARP table on a PC/Mac. For iOS, I found this SO answer to convert an IP address to a MAC address: Getting ARP table on iPhone/iPad
获取 SSID - 这与 MAC 地址问题无关。为此,请检查此 SO 答案:iPhone get SSID without private library。我不知道从其他答案中重新发布代码的礼仪是什么,所以我只是链接。
获取 MAC 地址 - 因为大多数网络运行在第 3 层(这被称为网络层 - http://en.wikipedia.org/wiki/OSI_model),而 MAC 地址在第 2 层(数据链路层)您首先需要获取 WiFi 接口网关的 IP 地址。只是一个旁注 - 该问题应表述为“如何获取当前连接的 WiFi 网络的 SSID 和 WiFi 路由器/网关的 MAC 地址”。任何人,要获取网关地址,请参阅此 SO 答案:如何在 iPhone 上获取 WIFI 网关地址?
一旦有了它,您需要使用 ARP(地址解析协议,这是所有网络用于将 IP 地址转换为 MAC 地址的协议)将其转换为 MAC 地址。网络接口不通过 IP 进行通信,它们通过 MAC 进行通信,因此这种情况实际上无处不在,一直在发生。在 PC/Mac 上访问 ARP 表实际上非常容易。对于 iOS,我发现这个 SO 答案将 IP 地址转换为 MAC 地址:在 iPhone/iPad 上获取 ARP 表