8

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!

Abu Bäkr
  • 313
  • 1
  • 2
  • 10
  • I am getting this error : Type 'NEHotspotNetwork' has no member 'fetchCurrent' – shaqir saiyed Nov 30 '20 at 07:20
  • Its working for me as I mentione here: https://stackoverflow.com/questions/63130232/cncopycurrentnetworkinfo-not-working-with-ios-14/65138156#65138156 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... – shaqir saiyed Dec 04 '20 at 05:02
  • Hmm - I am getting "NEHotspotNetwork nehelper sent invalid result code" on iOS15. I have the capability enabled and the location enabled – Jesper Kristiansen Nov 04 '22 at 02: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](https://www.createwithswift.com/using-the-locationbutton-in-swiftui-for-one-time-location-access/) and then call `NEHotspotNetwork.fetchCurrent` after location is accepted, then the SSID is returned correct – Jesper Kristiansen Nov 04 '22 at 03:04

2 Answers2

11

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?
 }
Jav Solo
  • 576
  • 1
  • 6
  • 15
Fahad Ali
  • 205
  • 1
  • 9
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)

user3441734
  • 16,722
  • 2
  • 40
  • 59