Search code examples
iosswiftiphoneios13ssid

How to fetch SSID in iOS device with iOS 13


I have an Objective-C iPhone application and Currently I am using below code to get the connected Wifi name. But it is not working in iOS 13. How can I get the connected Wifi SSID in iOS 13?

Currently I am using the below code in Swift:

public class SSID {
class func fetch() -> String {
    var currentSSID = ""
    if let interfaces = CNCopySupportedInterfaces() {
        for i in 0..<CFArrayGetCount(interfaces) {
            let interfaceName = CFArrayGetValueAtIndex(interfaces, i)
            let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
            let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
            if let interfaceData = unsafeInterfaceData as? [String: AnyObject] {
                currentSSID = interfaceData["SSID"] as! String
                let BSSID = interfaceData["BSSID"] as! String
                let SSIDDATA = interfaceData["SSIDDATA"] as! String
                debugPrint("ssid=\(currentSSID), BSSID=\(BSSID), SSIDDATA=\(SSIDDATA)")
            }
        }
    }
    return currentSSID
  }
}

But this code is returning nil in iOS 13, Thanks in advance!


Solution

  • 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.