Is possible connect to a specified WIFI in iOS?

I have a SSID and password for a WI-FI and i try to connect it automatically. I have found the solution for Android, but have anyone same solution for iOS?

Answers

  • I have same question, as per apple documentation ios 11 support to connect specific wifi with SSID and password.

    Is it possible from Xamarin form or Xamarin IOS to connect wifi with SSID and pssword?

  • diegoxleondiegoxleon ESMember ✭✭
    edited January 2

    Yes, you can connect to WPA networks with iOS 11 in 3 simple lines.

        public async Task<bool> ConnectWpa(string ssid, string password)
                {
                    try
                    {
                        var config = new NEHotspotConfiguration(ssid, password, false) {JoinOnce = true};
                        var configManager = new NEHotspotConfigurationManager();
                        await configManager.ApplyConfigurationAsync(config);
    
                        this.Log().Debug("Connected!!!!!");
    
                        return true;
                    }
                    catch (Foundation.NSErrorException error)
                    {
                        this.Log().Error(error.Message);
                        return false;
                    }
                    catch (Exception e)
                    {
                        this.Log().Exception(e);
                        return false;
                    }
                }
    
  • khoicvakhoicva Member ✭✭
    edited March 28

    Hi diegoxleon,
    In a Xamarin IOS project, when I connect to special wireless
    Althought i have enabled both Hotspot and Network extension in provision profile but one error still occur
    Error content is: “Exception of type ‘Foundation.NSErrorException’ was thrown.”

    My source like:

    public async Task ConnectToWifi_IOS(string ssid, string password)
    {
    try
    {
    var config = new NEHotspotConfiguration(ssid, password, false) { JoinOnce = true };
    await wifiManager.ApplyConfigurationAsync(config);
    Console.WriteLine(“Connected!”);
    }
    catch (Foundation.NSErrorException error)
    {
    Console.WriteLine(error.Message);
    return false;
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    return false;
    }
    return true;
    }

  • khoicvakhoicva Member ✭✭
    edited March 28

    Hi diegoxleon,

    In a Xamarin IOS project, when I connect to special wireless
    Althought i have enabled Hotspot in provision profile but one error still occur
    Error content is: “Exception of type ‘Foundation.NSErrorException’ was thrown.”

    My source like:

    public async Task ConnectToWifi_IOS(string ssid, string password)
    {
    try
    {
    var config = new NEHotspotConfiguration(ssid, password, false);
    await wifiManager.ApplyConfigurationAsync(config);
    Console.WriteLine(“Connected!”);
    }
    catch (Foundation.NSErrorException error)
    {
    Console.WriteLine(error.Message);
    return false;
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    return false;
    }
    return true;
    }

  • diegoxleondiegoxleon ESMember ✭✭

    @khoicva

    What´s the message of the error?

  • khoicvakhoicva Member ✭✭

    Hi diegoxleon,

    Error message is: “Exception of type ‘Foundation.NSErrorException’ was thrown.”

  • seanydaseanyda GBMember ✭✭✭✭✭

    @khoicva said:
    Hi diegoxleon,

    Error message is: “Exception of type ‘Foundation.NSErrorException’ was thrown.”

    Is that was the Console.WriteLine returned or the debugger? The Console.WriteLine should give more information about the exception.

  • diegoxleondiegoxleon ESMember ✭✭

    @khoicva You need to set a breakpoint on that line to get more info about the error. Otherwise it´s not possible to know the source of the problem

  • khoicvakhoicva Member ✭✭
    edited March 28

    Is that was the Console.WriteLine returned or the debugger?
    => It is debug result

    Here is StackTrace:

    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:152
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00037] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x00028] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156
    at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x00008] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128
    at System.Runtime.CompilerServices.TaskAwaiter.GetResult () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.8.0.20/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113
    at iLanCan.iOS.WifiConnectorIOS+d__4.MoveNext () [0x00054] in D:\Project\project_tuoi_cay_iot\03.Code\CrossPlatForm\iLanCan\iLanCan\iLanCan.iOS\WifiConnectorIOS.cs:30

Sign In or Register to comment.