Search code examples
swift5ios14nehotspothelper

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


I need to get ssid of currently connected network. The reason I need this is to enable my app to perform certain functions when connected to a specific network. Now I cant seem to figure it out as in how to get the ssid? I've read online and implemented following things.

-> Allowed user location

-> Logged in to Apple dev account and enabled Wifi access.

The function I am using is

  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)")
               }
           }
        }
   }

But it is giving the following error

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

What else am I missing here? Do i need to add anything else? Appreciate any help!


Solution

  • 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?
     }