Hello everyone, I tried to make my iPhone change the Wifi network using Xamarin.iOS.
I have already contacted the Xamarin service who told me that the programming is translated 1:1 with Swift and that the problem is an internal iPhone error.
My code is:
public class ConnectToWifi : IConnectToWifi
{
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:
ErrorDomain=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;
- Xcode Version 12.5.1;
- 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