6

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

Dom Bryan
  • 1,238
  • 2
  • 19
  • 39
  • did you enable Hotspot Configuration? – Leo Dabus Oct 30 '20 at 21:29
  • @LeoDabus I do not, you don't need too if you have location permissions enabled – Dom Bryan Oct 30 '20 at 21:36
  • I have never tried to use it with test flight but it might help https://stackoverflow.com/questions/60218223/get-mac-address-of-the-wifi-router-my-phone-is-connected-to-using-flutter/60218753#60218753 updated to iOS 14 – Leo Dabus Oct 30 '20 at 22:20
  • @LeoDabus what you mean by enabling hotspot configuration ? – shaqir saiyed Nov 02 '20 at 18:05
  • @shaqirsaiyed have you tried the code at link I've posted above? It worked for me. I did not try it in a test drive app so I don't know if it would solve your issue. https://www.dropbox.com/s/7dv44abllo7zdly/Router%20MacAddress.zip?dl=1 – Leo Dabus Nov 02 '20 at 18:19
  • @LeoDabus thanks. I've not tried it yet, but I think this should work for me too. The only thing which makes me wonder is whether this will be approved by apple as some suggest this needs extra permissions for the review process. is it so ? – shaqir saiyed Nov 02 '20 at 18:27
  • 1
    @shaqirsaiyed regarding approval for the APPStore I don't know. I think that for the test flight you won't have any trouble. – Leo Dabus Nov 02 '20 at 18:32
  • @LeoDabus okey thanks, I will try this. – shaqir saiyed Nov 03 '20 at 05:39

2 Answers2

4

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.

iUrii
  • 11,742
  • 1
  • 33
  • 48
  • Hi @iUrii, I am not sure this is ideal. Apples documentation says you need a special entitlement ```NEHotspotHelper is only useful for hotspot integration. There are both technical and business restrictions that prevent it from being used for other tasks, such as accessory integration or Wi-Fi based location. Before using NEHotspotHelper, you must first be granted a special entitlement (com.apple.developer.networking.HotspotHelper) by Apple. For more information, see Hotspot Helper Request.``` – Dom Bryan Nov 02 '20 at 11:42
  • Note that the `com.apple.developer.networking.HotspotHelper` entitlement requires a questionnaire is filled out and you might not be granted the entitlement for things like just getting the WiFi name – Dom Bryan Nov 02 '20 at 11:43
  • 1
    @DomBryan From SDK documentation of `fetchCurrent`: 1. application is using CoreLocation API and has user's authorization to access precise location so it's a this is a sufficient condition for fetching ssid (the same condition as for `CNCopyCurrentNetworkInfo`). – iUrii Nov 02 '20 at 13:48
  • @iUrii are you sure this is safe to use just to detect current WiFi information ? I am also getting nil for my wifi SSID in iOS 14 update.. – shaqir saiyed Nov 02 '20 at 18:07
  • Agree with @iUrii Method fetchCurrentWithCompletionHandler: * This method returns SSID and BSSID of the current Wi-Fi network when the * requesting application meets one of following 4 requirements -. * 1. application is using CoreLocation API and has user's authorization to access precise location. * 2. application has used NEHotspotConfiguration API to configure the current Wi-Fi network. * 3. application has active VPN configurations installed. * 4. application has active NEDNSSettingsManager configuration installed. – shaqir saiyed Nov 03 '20 at 05:54
  • I think you may have missed the point of my comment. The NEHotspotHelper is very specific and requires a questionnaire filled out in order to be granted permission to use it. If you try and do this now you will see `Hotspot Helper Request Thank you for your interest in the NEHotspotHelper API. The NEHotspotHelper interface allows Wi-Fi network implementers to facilitate connections to the large-scale wireless networks that they manage. For a complete explanation of all Wi-Fi management APIs available on iOS, see Technical Q&A QA1942: iOS Wi-Fi Management APIs.` This is not my use case – Dom Bryan Nov 03 '20 at 09:54
2

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()
      
Burtan
  • 41
  • 4