How to Get Available Wifi Network Name in iOS Using Swift

How to get SSID of currently connected Wifi Network in swift | iOS 14?

I have sorted out the way to get SSID of currently connected Wifi. Following are the pre-requisites to follow before writing the code.

-> You must have a paid developer account.

-> You must have a physical Device

-> You must enable Wifi-Entitlement by going to Target->Signing & Capabilities and adding Access WiFi Information or adding
<key>com.apple.developer.networking.wifi-info</key> <true/> directly to your entitlements file.

-> Allow location usage access from user

Then use this code in your class to get 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?
}

How to fetch SSID in iOS device with iOS 13

Using the code provided on iOS 14 I got the following error:

nehelper sent invalid result code [1] for Wi-Fi information request

Searching for that error took me to this question

Solution:

The requesting app must meet one of the following requirements:

The app uses Core Location, and has the user’s authorization to use
location information.

The app uses the NEHotspotConfiguration API to configure the current
Wi-Fi network.

The app has active VPN configurations installed.

An app that fails to meet any of the above requirements receives the
following return value:

An app linked against iOS 12 or earlier receives a dictionary with
pseudo-values. In this case, the SSID is Wi-Fi (or WLAN in the China
region), and the BSSID is 00:00:00:00:00:00.

An app linked against iOS 13 or later receives NULL.

Important

To use this function, an app linked against iOS 12 or later must
enable the Access WiFi Information capability in Xcode.

I also confirmed that this in fact works on iOS 14 once you request location permission.

import CoreLocation
import UIKit
import SystemConfiguration.CaptiveNetwork

final class ViewController: UIViewController {
var locationManager: CLLocationManager?

override func viewDidLoad() {
super.viewDidLoad()

locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization()
}

func getWiFiName() -> 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
}
}

extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedAlways {
let ssid = self.getWiFiName()
print("SSID: \(String(describing: ssid))")
}
}
}

Output: SSID: YaMomsWiFi

Don't forget to include the wifi entitlement, and the necessary keys in your plist for location permission.

iOS get current wlan network name

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

For this you'd use function CNCopyCurrentNetworkInfo

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

iOS Get list of all WiFi networks

This might be possible with NetworkExtension (available since iOS 8). But you need the com.apple.developer.networking.HotspotHelper-entitlement in you app to use these APIs. To get these entitlement, you have to contact Apple and describe, why you need it.

I haven't found any code examples, but maybe the documentation of NetworkExtension helps you.



Related Topics



Leave a reply



Submit