Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically connect to a WiFi network given the SSID and password

Tags:

ios

wifi

I am trying to replicate an existing Android Application that I made to iOS. In my application, I should be able to connect to a WiFi network given the SSID and the Password. The SSID and the Password can be hardcoded or can be inputted by the user. I was going through the internet to research how this can be done on iOS, however, it seems that this behavior is highly discouraged and that there's no way on doing this using public/documented libraries that won't get your application rejected by the App Store.

The thing is I'll be using this application personally and I don't plan on submitting it to the App Store so it's okay if I use external libraries. If my friends would want to use it, I could export an enterprise ipa and give them instructions on how to install it.

Upon searching, it seemed that MobileWifi.framework was a good candidate. However, it does not seem that there's a straightforward way of using this library to connect to a WiFi network given the SSID and the Password.

Is there anyone who has successfully tried to connect to a Wifi Network given the SSID and Password?

like image 360
Razgriz Avatar asked Mar 30 '16 08:03

Razgriz


People also ask

How do I connect to Wi-Fi using SSID?

Select Settings > Wi-Fi > Add Wi-Fi network. Enter the network name (SSID), security type, and password. Tap Connect. Your device connects to the network.

Is there a way to see the password of a network you are connected to?

In Network and Sharing Center, next to Connections, select your Wi-Fi network name. In Wi-Fi Status, select Wireless Properties. In Wireless Network Properties, select the Security tab, then select the Show characters check box. Your Wi-Fi network password is displayed in the Network security key box.


4 Answers

With iOS 11, Apple provided public API you can use to programmatically join a WiFi network without leaving your app.

The class you’ll need to use is called NEHotspotConfiguration.

To use it, you need to enable the Hotspot capability in your App Capabilities (Adding Capabilities). Quick working example :

NEHotspotConfiguration *configuration = [[NEHotspotConfiguration  alloc] initWithSSID:@“SSID-Name”]; configuration.joinOnce = YES;  [[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:nil]; 

This will prompt the user to join the “SSID-Name” WiFi network. It will stay connected to the WiFi until the user leaves the app.

This doesn't work with the simulator you need to run this code with an actual device to make it work.

More informations here : https://developer.apple.com/documentation/networkextension/nehotspotconfiguration

like image 135
Enthouan Avatar answered Oct 22 '22 05:10

Enthouan


connect wifi networks in iOS 11. You can connect wifi using ssid and password like following.

Enable Hotspot on App Id configure services

Enable Hotspot on App Id configure services

After Enable Hotspot Configuration

After Enable Hotspot Configuration

Swift 4.0 Code for iOS 11 Only:

import NetworkExtension 

...

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")     } } 
like image 25
Virajkumar Patel Avatar answered Oct 22 '22 06:10

Virajkumar Patel


Contrary to what you see here and other places, you can do it. It's hard to make pretty enough for normal users, but if doesn't have to be then it's really easy to code. It's an enterprise admin thing, but anyone can do it. Look up "Connection Profiles." Comcast does this in their iOS app to setup their hotspots for you, for example.

Basically, it's an XML document that you get the device to ingest via Safari or Mail. There's a lot of tricks to it and the user experience isn't great, but I can confirm that it works in iOS 10 and it doesn't require jailbreaking. You use Configurator to generate the XML, and I suggest this thread for serving it (but people don't find it because it's not specifically about WiFi), and this blog for querying if it's installed which is surprisingly nontrivial but does indeed work.

I've answered this question many times, but either don't have enough rep or am too dumb to figure out how to close questions as duplicate so better answers can be found more easily.

UPDATE: iOS 11 provides APIs for this functionality directly (finally!). See NEHotspotConfiguration. Our Xamarin C# code now looks like:

var config = new NEHotspotConfiguration(Ssid, Pw, false);
config.JoinOnce = false;
like image 44
Eliot Gillum Avatar answered Oct 22 '22 06:10

Eliot Gillum


Short answer, no.


Long answer :)

This question was asked many times:

  • Connect to WiFi programmatically in ios
  • connect to a specific wifi programmatically in ios with a given SSID and Password
  • Where do I find iOS Obj-C code to scan and connect to wifi (private API)
  • Programmatically auto-connect to WiFi iOS

The most interesting answer seems to be in the first link which points to a GitHub project: wifiAssociate. However someones in the third link explains that this doesn't work anymore with iOS8 so you might have hard time getting it running.
Furthermore the iDevice must be jailbroken.

like image 23
filaton Avatar answered Oct 22 '22 05:10

filaton