Error switching Wi-Fi connection on iPhone using Xamarin.iOS.

Giacomo Ferrera 21 Reputation points
2021-08-25T15:06:43.9+00:00

Hello everyone,

I tried to make my iPhone change the Wifi network using this code in Xamarin.iOS:

public class ConnectToWifi : IConnectToWifi
{
public string SSID;
public string Password;
public IPAddress ServerAddress;
int ServerPort = -1;
public bool isConnect = true;
public CFSocket Socket;
public CFSocket ServerSocket;
NEHotspotConfigurationManager wifiManager;

    public ConnectToWifi()
    {
        wifiManager = new NEHotspotConfigurationManager();
    }

    public async Task<bool> Connect(string Ip, int Port)
    {
        isConnect = false;
        ServerPort = Port;
        try
        {
            ServerAddress = new IPAddress(IPAddress.Parse(Ip).GetAddressBytes());
            Socket = new CFSocket();
            NEHotspotConfigurationManager.SharedManager.GetConfiguredSsids((listWifi) =>
                  {
                      if (listWifi != null)
                      {
                          foreach (string ssid_selected in listWifi)
                          {
                              if (ssid_selected == SSID)
                              {
                                  NEHotspotConfigurationManager.SharedManager.RemoveConfiguration(SSID);
                              }
                          }
                      }
                  });
            NEHotspotConfiguration wifiNetwork = new NEHotspotConfiguration(SSID, Password, false);
            wifiNetwork.JoinOnce = true;
            wifiManager.ApplyConfiguration(wifiNetwork, (error) =>
                {
                    if (error != null)
                    {
                        System.Diagnostics.Debug.Write(error.Description);
                    }
                    else
                    {
                        System.Diagnostics.Debug.Write("Connected");
                    }
                    isConnect = true;
                });
        }
        catch (CFSocketException exception)
        {
            System.Diagnostics.Debug.WriteLine("Ho ricevuto questo errore nella Connect: " + exception.Message);
            return isConnect;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Ho ricevuto questo errore nella Connect: " + ex.Message);
            return isConnect;
        }
        return isConnect;      
    }

Upon connection comes the wifiManager function.ApplyConfiguration returns the following error:

Error Domain=NEHotspotConfigurationErrorDomain Code=8 "internal error." UserInfo={NSLocalizedDescription=internal error.}

I have an iPhone X with iOS 14.6 and a MacBook with:

  • MacOS Big Sur 14.5, Visual Studio for Mac 8.10.7;
  • Xamarin.iOS 14.20.0.24 (Visual Studio Community);

In my AppID profile and in the Entitlements.plist file I have activated the following services:

  • Access WiFi Information;
  • Fonts;
  • Hotspot;
  • Network Extensions;

Thank you very much for helping!

Regards,

Giacomo Ferrera

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,299 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Junior Jiang 6 Reputation points
    2022-09-28T02:20:13.723+00:00

    Hi,It works in my project. The error means a internal error, you could restart your device to check whether it works.

    NEHotspotConfigurationErrorInternal = 8

    0 comments No comments