Getting the SSID of the CONNECTED WIFI on IOS 12- Xamarin

RoyaRoya USMember ✭✭

I could get the ssid of the connected wifi to my iphone. After Installing Xcode 10, Updating Visual Studio for Mac and Visual Studio 2017 and xamarin.forms, it returns me an empty ssid.

This is my piece of code for getting the ssid:

    public override string GetCurrentWiFi()
{
    String ssid = "";
    try
    {
        string[] supportedInterfaces;
        StatusCode status;
        if ((status = CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces)) != StatusCode.OK)
        {

        }
        else
        {
            foreach (var item in supportedInterfaces)
            {
                NSDictionary info;
                status = CaptiveNetwork.TryCopyCurrentNetworkInfo(item, out info);
                if (status != StatusCode.OK)
                {
                    continue;
                }
                ssid = info[CaptiveNetwork.NetworkInfoKeySSID].ToString();
            }
        }
    }
    catch
    {

    }
    return ssid;
}

I tried to add "Access WiFi Information" entitlement for iOS 12 app as it is mentioned here but the app still get an empty ssid:https://forums.xamarin.com/discussion/139476/adding-access-wifi-information-entitlement-for-ios-12-apps

I would be thankful if anyone could help.

Best Answers

  • RoyaRoya US ✭✭
    edited August 2019 Accepted Answer

    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".

Answers

  • RoyaRoya USMember ✭✭
    edited August 2019 Accepted Answer

    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".

  • andreGitaandreGita Member
    > @Roya said:
    > 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".

    I tried your solution but, even after added <key>NSLocationWhenInUseUsageDescription</key> in info.plist and called GetLocationConsent(), CaptiveNetwork fails and returns null.
    Any suggestions? CaptiveNetwork is also deprecated...
  • RoyaRoya USMember ✭✭

    @andreGita Can you put e try-catch and check what is the exception error?

  • andreGitaandreGita Member

    Ok sorry it works, I didn't wait the user authorization. But any alternative to CaptiveNetwork which is deprecated? The documentation suggests me to use NetworkExtensio.NEHotspotHelper

  • RoyaRoya USMember ✭✭

    @andreGita said:
    Ok sorry it works, I didn't wait the user authorization. But any alternative to CaptiveNetwork which is deprecated? The documentation suggests me to use NetworkExtensio.NEHotspotHelper

    I haven't tried it yet. I will let you know if I try it.

  • kefkef Member ✭✭

    @andreGita said:

    Actually i am new in xamarin , so Can you please share code to get ssid of currently connected wifi network in xamarin ios.

Sign In or Register to comment.