Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Last active April 4, 2018 12:10
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 dedeexe/96c38ad659779cad2dc0b1e7e66a3bb9 to your computer and use it in GitHub Desktop.
Save dedeexe/96c38ad659779cad2dc0b1e7e66a3bb9 to your computer and use it in GitHub Desktop.
Changing App Wifi
import Foundation
import NetworkExtension
import SystemConfiguration.CaptiveNetwork
public enum HotSpotError : Error, LocalizedError {
case featureNotSupported
public var localizedDescription: String {
return "This feature is available only for iOS 11 or later."
}
}
public class HotSpot {
private let ssid : String
private let password : String
public init(ssid:String, password:String) {
self.ssid = ssid
self.password = password
}
public func connect(completionHandler:((Error?)->Void)? = nil) throws {
if #available(iOS 11.0, *) {
let configuration = NEHotspotConfiguration(ssid: ssid, passphrase: password, isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { error in
completionHandler?(error)
}
return
}
throw HotSpotError.featureNotSupported
}
public static var currentSSID : String? {
let ssidKey = "SSID"
var ssid : String?
if let interfaces = CNCopySupportedInterfaces() as? [String] {
for interface in interfaces {
let ifaceName = interface as CFString
if let interfaceInfo = CNCopyCurrentNetworkInfo(ifaceName) as? [String:Any?] {
ssid = interfaceInfo[ssidKey] as? String
}
}
}
return ssid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment