3
3
4
4
import UIKit
5
5
import SystemConfiguration. CaptiveNetwork
6
+ import NetworkExtension
6
7
7
8
protocol SSIDOptionEditTableViewControllerDelegate : class {
8
9
func ssidOptionSaved( option: ActivateOnDemandViewModel . OnDemandSSIDOption , ssids: [ String ] )
@@ -39,9 +40,16 @@ class SSIDOptionEditTableViewController: UITableViewController {
39
40
selectedOption = option
40
41
selectedSSIDs = ssids
41
42
super. init ( style: . grouped)
42
- connectedSSID = getConnectedSSID ( )
43
43
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
+ }
45
53
}
46
54
47
55
required init ? ( coder aDecoder: NSCoder ) {
@@ -72,14 +80,14 @@ class SSIDOptionEditTableViewController: UITableViewController {
72
80
}
73
81
}
74
82
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 )
80
87
}
88
+ } else if let first = addSSIDRows. first, case . addConnectedSSID = first {
89
+ addSSIDRows. removeFirst ( )
81
90
}
82
- addSSIDRows. append ( . addNewSSID)
83
91
}
84
92
85
93
func updateTableViewAddSSIDRows( ) {
@@ -195,7 +203,7 @@ extension SSIDOptionEditTableViewController {
195
203
guard let self = self , let cell = cell else { return }
196
204
if let row = self . tableView. indexPath ( for: cell) ? . row {
197
205
self . selectedSSIDs [ row] = text
198
- self . loadAddSSIDRows ( )
206
+ self . updateCurrentSSIDEntry ( )
199
207
self . updateTableViewAddSSIDRows ( )
200
208
}
201
209
}
@@ -226,7 +234,7 @@ extension SSIDOptionEditTableViewController {
226
234
} else {
227
235
tableView. reloadRows ( at: [ indexPath] , with: . automatic)
228
236
}
229
- loadAddSSIDRows ( )
237
+ updateCurrentSSIDEntry ( )
230
238
updateTableViewAddSSIDRows ( )
231
239
case . addSSIDs:
232
240
assert ( editingStyle == . insert)
@@ -246,7 +254,7 @@ extension SSIDOptionEditTableViewController {
246
254
} else {
247
255
tableView. insertRows ( at: [ indexPath] , with: . automatic)
248
256
}
249
- loadAddSSIDRows ( )
257
+ updateCurrentSSIDEntry ( )
250
258
updateTableViewAddSSIDRows ( )
251
259
if newSSID. isEmpty {
252
260
if let selectedSSIDCell = tableView. cellForRow ( at: indexPath) as? EditableTextCell {
@@ -255,6 +263,31 @@ extension SSIDOptionEditTableViewController {
255
263
}
256
264
}
257
265
}
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
+ }
258
291
}
259
292
260
293
extension SSIDOptionEditTableViewController {
@@ -292,14 +325,3 @@ extension SSIDOptionEditTableViewController {
292
325
}
293
326
}
294
327
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