Programmatically Connecting to WiFi in iOS

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!

Replies

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"

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!

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"

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.

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"
Once the MDM install the configuration profile, what API can be used for connecting to the configured Wi-Fi profile. My requirement is to connect to that WiFi profile, extract some data and disconnect from it. This profile will be installed with Auto join off.

Thank you
  • @sktuser: Did you ever solve this? I have the exact same requirement. I can't find the API to join a known network programatically on an MDM managed device.

Add a Comment

If you’re already connected to the wifi, it won’t ask. Otherwise, there’s no way to bypass the permissions list.

Hey! We are currently using an MDM system (Jamf), is there a guide to know how to add the configuration profile with the wifi payload to join to a specific network SSID? We are succeeding to join the network by the user has to click accept every time he wants to join. It would be really good if we could avoid this cumbersome process.

We are currently using an MDM system

DevForums is focused on code-level issues, so you might have better luck asking this question over in Apple Support Community, run by Apple Support, and specifically the in Business and Education topic areas.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"