iOS 获取当前的 wlan 网络名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10967511/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:42:16  来源:igfitidea点击:

iOS get current wlan network name

iphoneobjective-ciosnetworkingwlan

提问by Chris

I am looking for a way to obtain information (at least the name) of the current connected wlan network in objective-c for iOS5.

我正在寻找一种方法来获取 iOS5 的 Objective-c 中当前连接的 wlan 网络的信息(至少是名称)。

I need this because we are currently developing an application that do not work in a particular network. In this network (on our university) the port is closed that we need to connect to the server. But there is another network also available and we want to tell the user that he has to switch the network if he is connected to the aforementioned one.

我需要这个,因为我们目前正在开发一个不能在特定网络中工作的应用程序。在这个网络(在我们的大学)中,我们需要连接到服务器的端口是关闭的。但是还有另一个网络可用,我们想告诉用户,如果他连接到上述网络,他必须切换网络。

I do not even know where to start. Does anyone have an idea or any hints?

我什至不知道从哪里开始。有没有人有想法或任何提示?

Thanks and regards

感谢致敬

采纳答案by Rok Jarc

From iOS >= 4.1 it's possible to obtain SSID of wireless network that device is currenctly connected to.

从 iOS >= 4.1 可以获取设备当前连接到的无线网络的 SSID。

For this you'd use function CNCopyCurrentNetworkInfo

为此,您将使用函数CNCopyCurrentNetworkInfo

Details on implemenation are available on SO: iPhone get SSID without private library

SO 上提供了有关实施的详细信息:iPhone 在没有私人图书馆的情况下获得 SSID

回答by nuynait

It is possible to get the current wifi information from the Captive Network. In the past, apple actually disabled this for a while, but they seems to re-enabled it due to strong request. It is also possible that they decide to close this in the future.

可以从 Captive Network 获取当前的 wifi 信息。过去,苹果实际上禁用了一段时间,但由于强烈要求,他们似乎重新启用了它。他们也有可能决定在未来关闭它。

The information we can get is BSSID, SSID, SSIDDATA. BSSIDis the unique address for wifi, SSIDis the current wifi name, SSIDDATAis the hex representation for the SSID.

我们可以得到的信息是BSSID, SSID, SSIDDATABSSID是 wifi 的唯一地址,SSID是当前的 wifi 名称,SSIDDATASSID.

For Swift 3.1:

对于 Swift 3.1:

func printCurrentWifiInfo() {
  if let interface = CNCopySupportedInterfaces() {
    for i in 0..<CFArrayGetCount(interface) {
      let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interface, i)
      let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
      if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString), let interfaceData = unsafeInterfaceData as? [String : AnyObject] {
        // connected wifi
        print("BSSID: \(interfaceData["BSSID"]), SSID: \(interfaceData["SSID"]), SSIDDATA: \(interfaceData["SSIDDATA"])")
      } else {
        // not connected wifi
      }
    }
  }
}

For Objective-C

对于 Objective-C

NSArray *interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();

for (NSString *name in interFaceNames) {
  NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);

  NSLog[@"wifi info: bssid: %@, ssid:%@, ssidData: %@", info[@"BSSID"], info[@"SSID"], info[@"SSIDDATA"]];
}

回答by Derek

As of iOS 12, you'll need to allow Wifi Information access in the capabilities.

从 iOS 12 开始,您需要在功能中允许 Wifi 信息访问。

From Apple:

来自苹果

To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID.

要在 iOS 12 及更高版本中使用此功能,请在 Xcode 中为您的应用启用访问 WiFi 信息功能。当您启用此功能时,Xcode 会自动将 Access WiFi Information 权利添加到您的权利文件和 App ID。