Created
December 9, 2021 03:41
-
-
Save 9wick/441996cc4614081b1623cab8f8f549fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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