10

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!

4
  • I am getting this error : Type 'NEHotspotNetwork' has no member 'fetchCurrent' Commented Nov 30, 2020 at 7:20
  • Its working for me as I mentione here: stackoverflow.com/questions/63130232/… The only thing is, I get this error:'' [] NEHotspotNetwork nehelper sent invalid result code [1] for Wi-Fi information request'' after few seconds with no network data... Commented Dec 4, 2020 at 5:02
  • Hmm - I am getting "NEHotspotNetwork nehelper sent invalid result code" on iOS15. I have the capability enabled and the location enabled Commented Nov 4, 2022 at 2:20
  • from looking at the UIViewController below, I realized that the location must called from the UI for the user to accept it. Using this location example and then call NEHotspotNetwork.fetchCurrent after location is accepted, then the SSID is returned correct Commented Nov 4, 2022 at 3:04

2 Answers 2

13

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?
 }
Sign up to request clarification or add additional context in comments.

2 Comments

You need to add the Access WiFi Information capability in TargetName->Signing&Capabilities. There is no need to add anything to the Info.plist related to WiFi.
it's working, but how to use it in SwiftUI? Any idea?
2

check https://developer.apple.com/contact/request/hotspot-helper/

Before using NEHotspotHelper, you must first be

granted a special entitlement (com.apple.developer.networking.HotspotHelper)

1 Comment

Not for fetchCurrent, as per developer.apple.com/forums/thread/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.