swift 如何获取当前连接的 Wifi 网络的 SSID | iOS14?
How to get SSID of currently connected Wifi Network in swift | iOS 14?
我需要获取当前连接网络的ssid。我需要这个的原因是让我的应用程序在连接到特定网络时能够执行某些功能。现在我似乎无法弄清楚如何获取 ssid?我已经在线阅读并实施了以下内容。
-> 允许的用户位置
-> 登录到 Apple 开发帐户并启用 Wifi 访问。
我使用的函数是
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedAlways {
NEHotspotNetwork.fetchCurrent { hotspotNetwork in
if let ssid = hotspotNetwork?.ssid {
print("SSID is \(ssid)")
}
}
}
}
但它给出了以下错误
NEHotspotNetwork nehelper sent invalid result code [5] for Wi-Fi information request
我还缺少什么?我还需要添加什么吗?感谢任何帮助!
检查https://developer.apple.com/contact/request/hotspot-helper/
在使用NEHotspotHelper之前,您必须先
granted a special entitlement
(com.apple.developer.networking.HotspotHelper)
我整理了获取当前连接的Wifi的SSID的方法。以下是编写代码之前要遵循的先决条件。
-> 您必须拥有付费开发者帐户。
-> 您必须有一个物理设备
-> 您必须通过转到 Target->Signing & Capabilities
并添加 Access WiFi Information
或添加来启用 Wifi-Entitlement
<key>com.apple.developer.networking.wifi-info</key> <true/>
直接到您的权利文件。
-> 允许来自用户的位置使用访问权限
然后在您的 class 中使用此代码获取 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?
}
我需要获取当前连接网络的ssid。我需要这个的原因是让我的应用程序在连接到特定网络时能够执行某些功能。现在我似乎无法弄清楚如何获取 ssid?我已经在线阅读并实施了以下内容。
-> 允许的用户位置
-> 登录到 Apple 开发帐户并启用 Wifi 访问。
我使用的函数是
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedAlways {
NEHotspotNetwork.fetchCurrent { hotspotNetwork in
if let ssid = hotspotNetwork?.ssid {
print("SSID is \(ssid)")
}
}
}
}
但它给出了以下错误
NEHotspotNetwork nehelper sent invalid result code [5] for Wi-Fi information request
我还缺少什么?我还需要添加什么吗?感谢任何帮助!
检查https://developer.apple.com/contact/request/hotspot-helper/
在使用NEHotspotHelper之前,您必须先
granted a special entitlement (com.apple.developer.networking.HotspotHelper)
我整理了获取当前连接的Wifi的SSID的方法。以下是编写代码之前要遵循的先决条件。
-> 您必须拥有付费开发者帐户。
-> 您必须有一个物理设备
-> 您必须通过转到 Target->Signing & Capabilities
并添加 Access WiFi Information
或添加来启用 Wifi-Entitlement
<key>com.apple.developer.networking.wifi-info</key> <true/>
直接到您的权利文件。
-> 允许来自用户的位置使用访问权限
然后在您的 class 中使用此代码获取 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?
}