如何使用Swift在iOS中获得所有wifi网络名称列表 [英] How to get available all wifi network name Listing in iOS using swift
问题描述
我有一个简单的问题,但我不知道如何实现.我需要创建一个应用程序,当用户单击某个网络iphone必须连接到该网络时,它可以获取可用wifi网络名称和信息的所有列表.我可以这样做吗?以及如何?
I have simple question but i dont know how to implement this. I need to create an app that can get all list of available wifi networks names and information when user click on some network iphone have to connect to this network. Can i do this? and How ?
推荐答案
不可能获得所有可用的wifi网络名称和信息. 但只能获得当前连接的网络的SSID
(SSID
只是网络名称的技术术语).
It is not possible to get all the available wifi networks names and information. BUT it is only possible to get the SSID
(SSID
is simply the technical term for a network name) of the network that you are currently connected to.
此类仅显示您连接的wifi网络名称-
This class will show only the wifi network name you are connected to -
import UIKit
import SystemConfiguration.CaptiveNetwork
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
override func viewDidLoad(){
super.viewDidLoad()
let ssid = self.getAllWiFiNameList()
print("SSID: \(ssid)")
}
func getAllWiFiNameList() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
break
}
}
}
return ssid
}
}
输出-(我连接到的网络名称)
OUTPUT- (Network name where i am connected to )
要对此进行测试,您需要将物理设备(iPhone)连接到您的PC.
To test this you need a physical device (iPhone) connected to your pc.
这篇关于如何使用Swift在iOS中获得所有wifi网络名称列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!