Connect to Wifi network from code – Swift
// | |
// HotspotHelper.swift | |
// Created by Pasca Alberto, IT on 03/11/2019. | |
// https://www.albertopasca.it/ | |
// | |
// MORE details on: | |
// https://www.albertopasca.it/whiletrue/connect-to-wifi-network-from-code-swift/ | |
// | |
import NetworkExtension | |
class HotspotHelper { | |
func connectToWifi( | |
wifiName: String, | |
wifiPassword: String, | |
wep: Bool, | |
completion: @escaping ((_ error: Bool) -> Void) ) | |
{ | |
self.clearConfiguredWifi() | |
let hotspotConfig = NEHotspotConfiguration(ssid: wifiName, passphrase: wifiPassword, isWEP: wep) | |
NEHotspotConfigurationManager.shared.apply(hotspotConfig) { (hotSpotError) in | |
if let _ = hotSpotError { | |
completion(false) | |
return | |
} | |
completion(true) | |
} | |
} | |
func disconnectAction( _ wifiName: String ) { | |
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: wifiName) | |
} | |
private func clearConfiguredWifi() { | |
NEHotspotConfigurationManager.shared.getConfiguredSSIDs { (wifiList) in | |
wifiList.forEach { | |
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: $0) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment