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:
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.
While this is not clearly documented. You don't need the special entitlement from NEHotspotHelper. You need:
locationManager = CLLocationManager()
locationManager?.requestAlwaysAuthorization()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With