iosxamarinxamarin.ioscaptivenetwork

Error getting network SSID in Xamarin.IOS


I added this key to Info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your Description</string>

And checked "Add access to wifi information" in Entitlements.plist.

I asked the user for permission to Location as well:

var manager = new CLLocationManager();
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
    manager.RequestWhenInUseAuthorization();

I click "allow", and then I would like to get the wifi information:

if (CLLocationManager.Status is CLAuthorizationStatus.Authorized || 
    CLLocationManager.Status is CLAuthorizationStatus.AuthorizedAlways || 
    CLLocationManager.Status is CLAuthorizationStatus.AuthorizedWhenInUse)
{
  if (CaptiveNetwork.TryGetSupportedInterfaces(out string[] supportedInterfaces) == StatusCode.OK)
  {
    foreach (var item in supportedInterfaces)
    {
      if (CaptiveNetwork.TryCopyCurrentNetworkInfo(item, out NSDictionary info) == StatusCode.OK)
      {
        var ssid = info[CaptiveNetwork.NetworkInfoKeySSID].ToString();
        System.Console.WriteLine(ssid);
      }
    }
  }
}

But the info variable is always null.

I would like to get the SSID and the protection level (WPA, WPA2 etc.) of the connected wifi network.

I use iPhone SE iOS 14.4. The software versions:


Solution

  • I need to participate in the Apple Developer Program, and enable Access WiFi Information capability at the apple developer portal.