Getting the SSID of the CONNECTED WIFI on IOS 12- Xamarin (Updated for iOS 13)

馋奶兔 提交于 2020-01-22 13:05:26

问题


I could get the ssid of the connected wifi to my iPhone. After Installing Xcode 10 and Updating Visual Studio for Mac and Visual Studio 2017, 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.

RESOLVED: I applied Access WIFI Information for my Apple ID. Then I regenerated my Provisioning profile again and open it in my Xcode. Do not forget to add Entitlements.plist in the Custom Entertainments in the ios Bundle Signing. Now it works correctly.

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


回答1:


To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app .

So you have to check Access WiFi Infomation in appid.



来源:https://stackoverflow.com/questions/52539526/getting-the-ssid-of-the-connected-wifi-on-ios-12-xamarin-updated-for-ios-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!