Skip to content

Instantly share code, notes, and snippets.

@9wick
Created December 9, 2021 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9wick/441996cc4614081b1623cab8f8f549fd to your computer and use it in GitHub Desktop.
Save 9wick/441996cc4614081b1623cab8f8f549fd to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// wifitest
//
// Created by 木戸 康平 on 2021/12/08.
//
import UIKit
import NetworkExtension
class ViewController: UIViewController {
@IBOutlet weak var connectButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func touchConnectButton(_ sender: UIButton) {
//printRetrievedWifiNetwork();
print("touchConnectButton");
connectToWifi(wifiName: "obniz-16438366",completion: { error in
print("error",error);
})
}
func connectToWifi(
wifiName: String,
completion: @escaping ((_ error: Bool) -> Void) )
{
self.clearConfiguredWifi()
let hotspotConfig = NEHotspotConfiguration(ssid: wifiName)
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