Get Ssid in Swift 2

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.

How do I get the current wifi network name after Yosemite in Swift?

So I figured it out. If you pass nil to the interfaceName, it will give you the currently connected wifi network. You can then call ssid() to get the name of the network.

Like so: CWInterface(interfaceName: nil).ssid()

Displaying SSID in iOS App using Swift

I figured out that it would be much easier to create a bridge from Swift to Objective-C.

Importing framework:

#import <SystemConfiguration/CaptiveNetwork.h>

Code to get the SSID of user's connected WiFi:

func getMAC()->(success:Bool,ssid:String,mac:String){

if let cfa: NSArray = CNCopySupportedInterfaces() {
for x in cfa {
if let dict = CFBridgingRetain(CNCopyCurrentNetworkInfo(x as! CFString)) {
let ssid = dict ["SSID"]!
let mac = dict["BSSID"]!
return (true, ssid as! String, mac as! String)
}
}
}
return (false,"","")
}

Print and display in a label when needed:

let x = getMAC()
if x.success {
MAClabel = x.mac
SSIDlabel = x.ssid
print(x.mac)
print (x.ssid)
}

I hope that those with this question would find this useful!

How to get Wifi SSID in iOS9 after CaptiveNetwork is deprecated and calls for Wifi name are already blocked

In the GM for iOS 9, it seems like this is enabled again. In fact, it's not even listed as deprecated in the online documentation, however the CaptiveNetwork header file does have the following:

CNCopySupportedInterfaces (void) __OSX_AVAILABLE_BUT_DEPRECATED_MSG(__MAC_10_8, __MAC_NA, __IPHONE_4_1, __IPHONE_9_0, CN_DEPRECATION_NOTICE);

So, it is working in the iOS 9 GM, but not sure for how long :)



Related Topics



Leave a reply



Submit