Last active
April 4, 2018 12:10
-
-
Save dedeexe/96c38ad659779cad2dc0b1e7e66a3bb9 to your computer and use it in GitHub Desktop.
Changing App Wifi
This file contains hidden or 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
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