Skip to content

Wifi SSID and BSSID

SSID (Service Set Identifier) is the human-readable name of the wifi network.

BSSID(Basic Service Set Identifier) is the MAC address of the wireless access point(AP) or router. The BSSID is typically represented as a series of hexadecimal digits, such as "00:14:22:01:23:45".

If you need to know the name of the device’s current Wi-Fi network, call fetchCurrent(completionHandler:) on iOS 14 or later, and call CNCopyCurrentNetworkInfo on older systems.

iOS 14 and later

1、Add Access Wi-Fi Information Entitlement to the Xcode project.

2、Require the precise location permission

This fetchCurrent(completionHandler:) method produces a non-nil NEHotspotNetwork object only when the requesting app meets at least one of the following criteria:

  1. The app is using the Core Location API and has user’s authorization to access precise location.
  2. The app used the NEHotspotConfiguration API to configure the current Wi-Fi network.
  3. The app has active VPN configurations installed.
  4. The app has an active NEDNSSettingsManager configuration installed.

3、Get currrent hotspot network

swift
import NetworkExtension
import CoreLocation

class WiFiInfo: NSObject, CLLocationManagerDelegate {
    private var locationManager: CLLocationManager?

    func fetchSSIDInfo() {
        NEHotspotHelper.register(options: nil, queue: DispatchQueue.main) { (cmd) in
            if let networkList = cmd.networkList {
                for network in networkList {
                    print("SSID: \(network.ssid)")
                    print("BSSID: \(network.bssid)")
                }
            }
        }
    }

    func requestLocationAuthorization() {
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            fetchSSIDInfo()
        }
    }
}

// Usage
let wifiInfo = WiFiInfo()
wifiInfo.requestLocationAuthorization()

Before iOS 14

The requesting app must meet one of the following requirements:

  1. The app is using the Core Location API and has user’s authorization to access precise location.
  2. The app used the NEHotspotConfiguration API to configure the current Wi-Fi network.
  3. The app has active VPN configurations installed.
  4. The app has an active NEDNSSettingsManager configuration installed.
swift
import SystemConfiguration.CaptiveNetwork
import CoreLocation

class WiFiInfo: NSObject, CLLocationManagerDelegate {
    private var locationManager: CLLocationManager?

    func fetchSSIDInfo() -> [String: Any]? {
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
            for interface in interfaces {
                if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                    return interfaceInfo as? [String: Any]
                }
            }
        }
        return nil
    }

    func requestLocationAuthorization() {
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestWhenInUseAuthorization()
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            if let ssidInfo = fetchSSIDInfo() {
                if let ssid = ssidInfo[kCNNetworkInfoKeySSID as String] as? String,
                   let bssid = ssidInfo[kCNNetworkInfoKeyBSSID as String] as? String {
                    print("SSID: \(ssid)")
                    print("BSSID: \(bssid)")
                }
            }
        }
    }
}

// Usage
let wifiInfo = WiFiInfo()
wifiInfo.requestLocationAuthorization()

References