5 Replies
      Latest reply on Nov 30, 2018 12:55 AM by eskimo
      beautyofscience@icloud.com Level 1 Level 1 (0 points)

        Hi,

         

        I create application "Connecting to WiFi in iOS"

         

        Code to connecting:

        • Swift

        let configuration = NEHotspotConfiguration.init(ssid: "SSIDname", passphrase: "Password", isWEP: false)
        configuration.joinOnce = true

        NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
          if error != nil {
          if error?.localizedDescription == "already associated."
          {
          print("Connected")
          }
          else{
          print("No Connected")
          }
          }
          else {
          print("Connected")
          }
        }

        • and Xamarin

        [assembly: Dependency(typeof(WifiConnector))]

        namespace WiFiManager.iOS

        {

            public class WifiConnector : IWifiConnector

            {

                public void ConnectToWifi(string ssid, string password)

                {

                    var wifiManager = new NEHotspotConfigurationManager();

                    var wifiConfig = new NEHotspotConfiguration(ssid, password, false);

         

                    wifiManager.ApplyConfiguration(wifiConfig, (error) =>

                    {

                        if (error != null)

                        {

                            Console.WriteLine($"Error while connecting to WiFi network {ssid}: {error}");

                        }

                    });

                }

            }

        }

         

        Everything works fine but iOS always asks a question "Wants to Join Wi-Fi Network".

        Is there any possibility that it would not ask? For my application, this popup is a problem. Maybe list of preferred network?

        Thank you in advance!

        • Re: Programmatically Connecting to WiFi in iOS
          eskimo Apple Staff Apple Staff (12,265 points)

          iOS always asks a question "Wants to Join Wi-Fi Network".

          That’s correct.  Wi-Fi configuration creation must be authorised by the user.

          Is there any possibility that it would not ask?

          Not via this API.

          If you’re working in a managed environment that you can have MDM install a configuration profile with a Wi-Fi payload (com.apple.wifi.managed) that does not require specific user approval (although joining the MDM server is something the user must authorise).  See the Configuration Profile Reference for details.

          IMPORTANT Configuration profile based solutions are only appropriate for managed environments.  If you intend to deploy to normal users via the App Store then NEHotspotConfigurationManager is the right choice.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Programmatically Connecting to WiFi in iOS
              beautyofscience@icloud.com Level 1 Level 1 (0 points)

              iOS always asks a question "Wants to Join Wi-Fi Network".

              "That’s correct.  Wi-Fi configuration creation must be authorised by the user."

               

              Even if I was already connected to this network earlier? This is my preferred/remembered network.

               

              Thank you eskimo!

                • Re: Programmatically Connecting to WiFi in iOS
                  eskimo Apple Staff Apple Staff (12,265 points)

                  Even if I was already connected to this network earlier?

                  Yes.  You are programmatically changing system-wide settings, which is something that iOS does not generally allow without user consent.

                  This is my preferred/remembered network.

                  Generally iOS will join the known networks without any special effort on your part.  Can you explain your workflow in more detail?

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"

                    • Re: Programmatically Connecting to WiFi in iOS
                      beautyofscience@icloud.com Level 1 Level 1 (0 points)

                      I create an application. My device (iphone or ipad) connects to another device using the wifi. My application has two functions/modules.

                      In the first one needs internet (via wifi) and in the second connection with another device via wifi. The switching must be automatic without popup and questions "Wants to Join Wi-Fi Network". The connection (internet or another device) must be correct for the application module in which we are actually.

                        • Re: Programmatically Connecting to WiFi in iOS
                          eskimo Apple Staff Apple Staff (12,265 points)

                          First up, terminology:

                          • I’m going to call your another device an accessory, so I can keep using device to mean iOS device.

                          • I’m going to assume the terms from my Wi-Fi Fundamentals post.

                          The switching must be automatic without popup and questions "Wants to Join Wi-Fi Network".

                          That is, alas, an infeasible goal.  As things current stand switching Wi-Fi configurations always requires user authorisation.

                          The only wiggle room you have here is the joinOnce property.  By setting joinOnce you prevent NEHotspotConfigurationManager from creating a persistent configuration for your accessory.  This has two benefits:

                          • If the user moves your app to the background then iOS will automatically disassociate from your accessory’s network, at which point it will start its usual Wi-Fi machinery and typically re-join the original network.

                          • If your accessory’s AP goes offline then, again, iOS will start its usual Wi-Fi machinery and typically re-join the original network.  Later on, when if your accessory’s AP comes back online, iOS won’t rejoin it because there’s no persistent configuration.

                          You could probably use this second point to get the device back on to the original network without any user authorisation, but getting it on to your accessory’s network in the first place is always going to need authorisation.

                          Share and Enjoy

                          Quinn “The Eskimo!”
                          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                          let myEmail = "eskimo" + "1" + "@apple.com"