我已经整理出了获取当前连接的 Wifi 的 SSID 的方法。以下是编写代码之前要遵循的先决条件。
-> 你必须有一个付费的开发者账户。
-> 你必须有一个物理设备
-> 您必须通过直接Target->Signing & Capabilities
添加Access WiFi Information
或添加
<key>com.apple.developer.networking.wifi-info</key> <true/>
到您的权利文件来启用 Wifi 权利。
-> 允许用户访问位置信息
然后在您的班级中使用此代码来获取 SSID。
import UIKit
import SystemConfiguration.CaptiveNetwork
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentNetworkInfos: Array<NetworkInfo>? {
get {
return SSID.fetchNetworkInfo()
}
}
let ssidLabel:UILabel = {
let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()
let bssidLabel:UILabel = {
let lbl = UILabel()
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
view.addSubview(ssidLabel)
view.addSubview(bssidLabel)
NSLayoutConstraint.activate([
ssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
ssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
bssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
bssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 20),
])
if #available(iOS 13.0, *) {
let status = CLLocationManager.authorizationStatus()
if status == .authorizedWhenInUse {
updateWiFi()
} else {
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
} else {
updateWiFi()
}
}
func updateWiFi() {
print("SSID: \(currentNetworkInfos?.first?.ssid ?? "")")
if let ssid = currentNetworkInfos?.first?.ssid {
ssidLabel.text = "SSID: \(ssid)"
}
if let bssid = currentNetworkInfos?.first?.bssid {
bssidLabel.text = "BSSID: \(bssid)"
}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
updateWiFi()
}
}
}
public class SSID {
class func fetchNetworkInfo() -> [NetworkInfo]? {
if let interfaces: NSArray = CNCopySupportedInterfaces() {
var networkInfos = [NetworkInfo]()
for interface in interfaces {
let interfaceName = interface as! String
var networkInfo = NetworkInfo(interface: interfaceName,
success: false,
ssid: nil,
bssid: nil)
if let dict = CNCopyCurrentNetworkInfo(interfaceName as CFString) as NSDictionary? {
networkInfo.success = true
networkInfo.ssid = dict[kCNNetworkInfoKeySSID as String] as? String
networkInfo.bssid = dict[kCNNetworkInfoKeyBSSID as String] as? String
}
networkInfos.append(networkInfo)
}
return networkInfos
}
return nil
}
}
struct NetworkInfo {
var interface: String
var success: Bool = false
var ssid: String?
var bssid: String?
}