Programmatically Auto-Connect to Wifi iOS

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

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

iOS Swift connect WiFi programmatically and distinguish between bad password and no WiFi in range

Quickly tried it and it is perfectly reproducible it seems.
Further research revealed that it is an Apple error.
So the answer is: You are nothing doing wrong, it's a bug.

Reasons seems to be:

...the errors that you see via the completion handler are those errors related to the framework itself. Once the request has made it past the Network Extension framework, down to the Wi-Fi subsystem, errors are displayed to the user rather than delivered to your completion handler. Whether that’s a bug is up for debate.

Later down one can read:

So my 2 bugs (46628017 and 46579891) regarding the NEHotspot marked as duplicated of 42919071

see the whole discussion here:
https://forums.developer.apple.com/thread/96834

Some Experiment

There is this nice SO answer: https://stackoverflow.com/a/5198968/2331445

It shows how to access information about the currently connected WIFI.

As the author explains, you need to add the Access WiFi Information capability for iOS > 12. A test using the getConnectedWifiInfo method from the above SO answer for an experiment might look like this:

import UIKit
import NetworkExtension
import SystemConfiguration.CaptiveNetwork

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func onConnect(_ sender: Any) {
let configuration = NEHotspotConfiguration.init(ssid: "somessid", passphrase: "somepassphrase", isWEP: false)
configuration.joinOnce = true

NEHotspotConfigurationManager.shared.apply(configuration) { [weak self] (error) in
print("error is \(String(describing: error))")
if let error = error {
let nsError = error as NSError
if nsError.domain == "NEHotspotConfigurationErrorDomain" {
if let configError = NEHotspotConfigurationError(rawValue: nsError.code) {
switch configError {
case .invalidWPAPassphrase:
print("password error: \(error.localizedDescription)")
case .invalid, .invalidSSID, .invalidWEPPassphrase,
.invalidEAPSettings, .invalidHS20Settings, .invalidHS20DomainName, .userDenied, .pending, .systemConfiguration, .unknown, .joinOnceNotSupported, .alreadyAssociated, .applicationIsNotInForeground, .internal:
print("other error: \(error.localizedDescription)")
@unknown default:
print("later added error: \(error.localizedDescription)")
}
}
} else {
print("some other error: \(error.localizedDescription)")
}
} else {
print("perhaps connected")

self?.printWifiInfo()
}
}

}

@IBAction func onInfo(_ sender: Any) {
self.printWifiInfo()
}

private func printWifiInfo() {
print("printWifiInfo:")
if let wifi = self.getConnectedWifiInfo() {
if let connectedSSID = wifi["SSID"] {
print("we are currently connected with \(connectedSSID)")
}
print("further info:")
for (k, v) in wifi {
print(". \(k) \(v)")
}
}
print()
}

private func getConnectedWifiInfo() -> [AnyHashable: Any]? {
if let ifs = CFBridgingRetain( CNCopySupportedInterfaces()) as? [String],
let ifName = ifs.first as CFString?,
let info = CFBridgingRetain( CNCopyCurrentNetworkInfo((ifName))) as? [AnyHashable: Any] {
return info
}
return nil
}

}

And indeed if I run this code I get as result:

error is nil
perhaps connected
printWifiInfo:
we are currently connected with somessid
further info:
. SSIDDATA <11111111 22222222 333331333 44444444 55>
. BSSID 70:33:ab:cd:ef:22
. SSID somessid

The author of this SO answer also shows methods for older iOS versions.

Connect to WiFi programmatically in ios

I ran into this issue last year and after quite a bit of digging I came across a helpful example called SOLStumbler. For this app to work your device must be jailbroken. I modified SOLStumbler and threw up a sample app on github. It uses the IPConfiguration bundle to access Apple80211 framework, which allows you to associate to a network. The readme contains the custom build instructions.

https://github.com/devinshively/wifiAssociate

Is it possible to connect to WiFi automatically by capturing QR-Code?

We have four steps to deal with this issue.

1.Capture the QR-Code which contains the specific WiFi hotspot information(SSID, Password, Encryption Type).

2.Convert JSON data of QR-Code to Dictionary.

3.Get the SSID, Password and Encryption type value from before-mentioned Dictionary.

4.Use NEHotspotConfiguration of Apple API to set our SSID, Password and Encryption type and connect to the specific WiFi Hotspot.

p.s. Your app needs the signing certificate to active some function of your project and Apple API.

Let's see what have I done here:

func handleScan(result: Result<String, CodeScannerView.ScanError>) {
switch result {
case .success(let code):
let data_code = code.data(using: .utf8)
do {
let dict_code = try JSONSerialization.jsonObject(with: data_code!, options: .allowFragments) as! [String : Any]
let wifi_ssid = dict_code["S"] as! String
let wifi_pwd = dict_code["P"] as! String
let wifi_type = dict_code["T"] as! String

let configuration = NEHotspotConfiguration.init(ssid: wifi_ssid, passphrase: wifi_pwd, isWEP: self.checkWifiType(type: wifi_type))
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) {
(error) in
if error != nil {
if let errorStr = error?.localizedDescription {
print("Error Information:\(errorStr)")
}
if (error?.localizedDescription == "already associated.") {
print("Connected!")
} else {
print("No Connected!")
}
} else {
print("Connected!")
}
}

print("Dict_Code:\(dict_code)")
} catch (let error) {
print("JSONSerial... Convert Error:\(error.localizedDescription)")
}

case .failure(let error):
self.connectionStatus = "Scanning failed!"
}
}

After doing this, I finally can scan my own QR-Code and connect to the specific WiFi Hotspot.

iOS swift programatically disable wifi auto join option

Short Answer: No.

Longer: This would violate the general idea of a sandbox if a process within the sandbox changes the system outside.

Think the other way around: You can check the SCNetworkReachability flags about the current connection and respond within your app.



Related Topics



Leave a reply



Submit