Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
Created November 8, 2013 14:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dannycabrera/7371787 to your computer and use it in GitHub Desktop.
Save dannycabrera/7371787 to your computer and use it in GitHub Desktop.
Xamarin.iOS GetSSID
public static string GetSSID(bool withMacAddress = true)
{
try {
NSDictionary dict;
var status = CaptiveNetwork.TryCopyCurrentNetworkInfo ("en0", out dict);
if (status == StatusCode.NoKey)
return "";
var bssid = dict [CaptiveNetwork.NetworkInfoKeyBSSID];
var ssid = dict [CaptiveNetwork.NetworkInfoKeySSID];
//var ssiddat = dict [CaptiveNetwork.NetworkInfoKeySSIDData];
if (withMacAddress)
return string.Format ("{0} [{1}]", ssid, bssid);
else
return ssid.ToString ();
/*
foreach (string intf in CaptiveNetwork.GetSupportedInterfaces()) {
NSDictionary dict2;
CaptiveNetwork.TryCopyCurrentNetworkInfo (intf, out dict2);
//if (status == StatusCode.NoKey)
// return "";
var bssid2 = dict [CaptiveNetwork.NetworkInfoKeyBSSID];
var ssid2 = dict [CaptiveNetwork.NetworkInfoKeySSID];
var ssiddat2 = dict [CaptiveNetwork.NetworkInfoKeySSIDData];
}
*/
} catch {
return "";
}
}
@sheraz16
Copy link

sheraz16 commented Nov 6, 2019

Updated for iOS 13: Apple announced that iOS 13, the CNCopyCurrentNetworkInfo API will no longer return valid Wi-Fi SSID and BSSID information.

If your app requires valid Wi-Fi SSID and BSSID information to function, you can do the following: · For accessory setup apps, use the NEHotSpotConfiguration API, which now has the option to pass a prefix of the SSID hotspot your app expects to connect to. · For other types of apps, use the CoreLocation API to request the user’s consent to access location information.

So, I updated the above solution in the following way:

Add this key to your info.plist:

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

Use the CoreLocation API to request the user’s consent to access location information.

private void GetLocationConsent()
{
    var manager = new CLLocationManager();
    manager.AuthorizationChanged += (sender, args) => {
        Console.WriteLine("Authorization changed to: {0}", args.Status);
    };
    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        manager.RequestWhenInUseAuthorization();

}

Call the GetLocationConsent() function before calling the "CaptiveNetwork".

@dannycabrera
Copy link
Author

Thanks for the input @sheraz16.

@jcubero
Copy link

jcubero commented Jun 16, 2020

Thank you, Sheraz16 I implemented it and it worked on my project, is there a way to get the signal strength of the WIFI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment