// // HMG_GUEST.swift // HMG-iOS-Wifi // // Created by ZiKambrani on 23/03/1442 AH. // Copyright © 1442 ZiKambrani. All rights reserved. // import UIKit import NetworkExtension import SystemConfiguration.CaptiveNetwork fileprivate let SSID = "HMG-MobileApp" class HMG_GUEST{ static let shared = HMG_GUEST() private var complete:((_ status:Bool, _ message:String) -> Void)! func connect(completion:@escaping ((_ status:Bool, _ message:String) -> Void)){ complete = completion if isAlreadyConnected() { FlutterText.with(key: "alreadyConnectedHmgNetwork") { (localized) in self.complete(true, localized ) } }else{ connect() } } private func connect() { let hotspotConfig = NEHotspotConfiguration(ssid: "\(SSID)") hotspotConfig.joinOnce = false NEHotspotConfigurationManager.shared.apply(hotspotConfig) {[weak self] (error) in guard let self = self else { return; } if let error = error { FlutterText.with(key: "errorConnectingHmgNetwork") { (localized) in self.complete(false, localized ) } }else{ _ = Timer.scheduledTimer(withTimeInterval: 10, repeats: false) { (timer) in let connected = self.isAlreadyConnected() let message = connected ? "successConnectingHmgNetwork" : "failedConnectingHmgNetwork" FlutterText.with(key: message) { (localized) in self.complete(false, localized ) } } } } // NSMutableURLRequest(url: URL(string: "www.google.com")!).bind(to: command) // NEHotspotHelper.register(options: [:], queue: DispatchQueue.global()) { (command) in // command // print(command) // } } private func isAlreadyConnected() -> Bool{ var currentSSID: String? if let interfaces = CNCopySupportedInterfaces() as NSArray? { for interface in interfaces { if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { currentSSID = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String break } } } print("CurrentConnectedSSID: \(currentSSID)") return currentSSID == SSID } }