33
44import UIKit
55import SystemConfiguration. CaptiveNetwork
6+ import NetworkExtension
67
78protocol SSIDOptionEditTableViewControllerDelegate : class {
89 func ssidOptionSaved( option: ActivateOnDemandViewModel . OnDemandSSIDOption , ssids: [ String ] )
@@ -39,9 +40,16 @@ class SSIDOptionEditTableViewController: UITableViewController {
3940 selectedOption = option
4041 selectedSSIDs = ssids
4142 super. init ( style: . grouped)
42- connectedSSID = getConnectedSSID ( )
4343 loadSections ( )
44- loadAddSSIDRows ( )
44+ addSSIDRows. removeAll ( )
45+ addSSIDRows. append ( . addNewSSID)
46+
47+ getConnectedSSID { [ weak self] ssid in
48+ guard let self = self else { return }
49+ self . connectedSSID = ssid
50+ self . updateCurrentSSIDEntry ( )
51+ self . updateTableViewAddSSIDRows ( )
52+ }
4553 }
4654
4755 required init ? ( coder aDecoder: NSCoder ) {
@@ -72,14 +80,14 @@ class SSIDOptionEditTableViewController: UITableViewController {
7280 }
7381 }
7482
75- func loadAddSSIDRows( ) {
76- addSSIDRows. removeAll ( )
77- if let connectedSSID = connectedSSID {
78- if !selectedSSIDs. contains ( connectedSSID) {
79- addSSIDRows. append ( . addConnectedSSID( connectedSSID: connectedSSID) )
83+ func updateCurrentSSIDEntry( ) {
84+ if let connectedSSID = connectedSSID, !selectedSSIDs. contains ( connectedSSID) {
85+ if let first = addSSIDRows. first, case . addNewSSID = first {
86+ addSSIDRows. insert ( . addConnectedSSID( connectedSSID: connectedSSID) , at: 0 )
8087 }
88+ } else if let first = addSSIDRows. first, case . addConnectedSSID = first {
89+ addSSIDRows. removeFirst ( )
8190 }
82- addSSIDRows. append ( . addNewSSID)
8391 }
8492
8593 func updateTableViewAddSSIDRows( ) {
@@ -195,7 +203,7 @@ extension SSIDOptionEditTableViewController {
195203 guard let self = self , let cell = cell else { return }
196204 if let row = self . tableView. indexPath ( for: cell) ? . row {
197205 self . selectedSSIDs [ row] = text
198- self . loadAddSSIDRows ( )
206+ self . updateCurrentSSIDEntry ( )
199207 self . updateTableViewAddSSIDRows ( )
200208 }
201209 }
@@ -226,7 +234,7 @@ extension SSIDOptionEditTableViewController {
226234 } else {
227235 tableView. reloadRows ( at: [ indexPath] , with: . automatic)
228236 }
229- loadAddSSIDRows ( )
237+ updateCurrentSSIDEntry ( )
230238 updateTableViewAddSSIDRows ( )
231239 case . addSSIDs:
232240 assert ( editingStyle == . insert)
@@ -246,7 +254,7 @@ extension SSIDOptionEditTableViewController {
246254 } else {
247255 tableView. insertRows ( at: [ indexPath] , with: . automatic)
248256 }
249- loadAddSSIDRows ( )
257+ updateCurrentSSIDEntry ( )
250258 updateTableViewAddSSIDRows ( )
251259 if newSSID. isEmpty {
252260 if let selectedSSIDCell = tableView. cellForRow ( at: indexPath) as? EditableTextCell {
@@ -255,6 +263,31 @@ extension SSIDOptionEditTableViewController {
255263 }
256264 }
257265 }
266+
267+ private func getConnectedSSID( completionHandler: @escaping ( String ? ) -> Void ) {
268+ #if targetEnvironment(simulator)
269+ completionHandler ( " Simulator Wi-Fi " )
270+ #else
271+ if #available( iOS 14 , * ) {
272+ NEHotspotNetwork . fetchCurrent { hotspotNetwork in
273+ completionHandler ( hotspotNetwork? . ssid)
274+ }
275+ } else {
276+ if let supportedInterfaces = CNCopySupportedInterfaces ( ) as? [ CFString ] {
277+ for interface in supportedInterfaces {
278+ if let networkInfo = CNCopyCurrentNetworkInfo ( interface) {
279+ if let ssid = ( networkInfo as NSDictionary ) [ kCNNetworkInfoKeySSID as String ] as? String {
280+ completionHandler ( !ssid. isEmpty ? ssid : nil )
281+ return
282+ }
283+ }
284+ }
285+ }
286+
287+ completionHandler ( nil )
288+ }
289+ #endif
290+ }
258291}
259292
260293extension SSIDOptionEditTableViewController {
@@ -292,14 +325,3 @@ extension SSIDOptionEditTableViewController {
292325 }
293326}
294327
295- private func getConnectedSSID( ) -> String ? {
296- guard let supportedInterfaces = CNCopySupportedInterfaces ( ) as? [ CFString ] else { return nil }
297- for interface in supportedInterfaces {
298- if let networkInfo = CNCopyCurrentNetworkInfo ( interface) {
299- if let ssid = ( networkInfo as NSDictionary ) [ kCNNetworkInfoKeySSID as String ] as? String {
300- return !ssid. isEmpty ? ssid : nil
301- }
302- }
303- }
304- return nil
305- }
0 commit comments