Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Getting current WiFi SSID is always nil on Testflight

Below is the code I use to get my current WiFi SSID and display it in my app.

I have location permissions set to always, as well as the required Privacy info.plist values. I also have the Access WiFi Information capability added to my project. When I build the app from Xcode to my iPhone (not simulator), it works fine, I can see my WiFi SSID. However, when I distribute the app through Testflight it no longer works, it is returning nothing.

import SystemConfiguration.CaptiveNetwork

private func getWiFiSsid() -> 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
    }

Below is a screenshot of the entitlements that I unpackages from the ipa file, showing that I do have the Access WiFi Information set: WiFi entitlement

like image 675
Dom Bryan Avatar asked Oct 30 '20 14:10

Dom Bryan


2 Answers

Since CNCopyCurrentNetworkInfo is deprecated from iOS 14 (https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo) consider migrating to NEHotspotNetwork.fetchCurrent and we can use this method with user's authorization to access precise location e.g.:

import CoreLocation
import NetworkExtension

var locationManager: CLLocationManager?

...

locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.requestAlwaysAuthorization()

...

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways || status == .authorizedWhenInUse {
        NEHotspotNetwork.fetchCurrent { hotspotNetwork in
            if let ssid = hotspotNetwork?.ssid {
                print(ssid)
            }
        }
    }
}

NOTE: you have to set Access WiFi Information to YES in your entitlements file ,Privacy - Location Always and When In Use Usage Description and Privacy - Location When In Use Usage Description in you Info.plist as well.

like image 65
iUrii Avatar answered Oct 16 '22 10:10

iUrii


While this is not clearly documented. You don't need the special entitlement from NEHotspotHelper. You need:

  • Access WiFi Information = YES (in entitlements, either add it manually or via xcode Signing and Capabilities of your target)
  • Privacy - Location Always and When In Use Usage Description (in Info.plist)
  • Privacy - Location When In Use Usage Description (in Info.plist)
  • Location access granted via
    • locationManager = CLLocationManager()
      locationManager?.requestAlwaysAuthorization()
      
like image 41
Burtan Avatar answered Oct 16 '22 12:10

Burtan