Skip to content

Commit 3fa8963

Browse files
authored
Migrate from CNCopySupportedInternet to NEHotspotNetwork (#3446)
1 parent 057c9d0 commit 3fa8963

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

Sources/Shared/Environment/ConnectivityWrapper.swift

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22
#if os(iOS)
33
import CoreTelephony
44
import Reachability
5-
import SystemConfiguration.CaptiveNetwork
65
#endif
76
import Communicator
7+
import NetworkExtension
88

99
/// Wrapper around CoreTelephony, Reachability
1010
public class ConnectivityWrapper {
@@ -56,34 +56,25 @@ public class ConnectivityWrapper {
5656
}
5757
self.hasWiFi = { true }
5858
self.currentWiFiSSID = {
59-
#if targetEnvironment(simulator)
60-
return "Simulator"
61-
#endif
62-
63-
guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
64-
for interface in interfaces {
65-
guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? else {
66-
continue
67-
}
68-
return interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
69-
}
70-
return nil
59+
nil
7160
}
7261
self.currentWiFiBSSID = {
73-
guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
74-
for interface in interfaces {
75-
guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as CFString) as NSDictionary? else {
76-
continue
77-
}
78-
return interfaceInfo[kCNNetworkInfoKeyBSSID as String] as? String
79-
}
80-
return nil
62+
nil
8163
}
8264
self.connectivityDidChangeNotification = { .reachabilityChanged }
8365
self.simpleNetworkType = { reachability?.getSimpleNetworkType() ?? .unknown }
8466
self.cellularNetworkType = { reachability?.getNetworkType() ?? .unknown }
8567
self.currentNetworkHardwareAddress = { nil }
8668
self.networkAttributes = { [:] }
69+
70+
syncNetworkInformation()
71+
72+
NotificationCenter.default.addObserver(
73+
self,
74+
selector: #selector(connectivityDidChange(_:)),
75+
name: .reachabilityChanged,
76+
object: nil
77+
)
8778
}
8879
#else
8980
init() {
@@ -111,4 +102,26 @@ public class ConnectivityWrapper {
111102
CTTelephonyNetworkInfo().serviceCurrentRadioAccessTechnology
112103
}
113104
#endif
105+
106+
@objc private func connectivityDidChange(_ note: Notification) {
107+
syncNetworkInformation()
108+
}
109+
110+
private func syncNetworkInformation() {
111+
NEHotspotNetwork.fetchCurrent { hotspotNetwork in
112+
Current.Log
113+
.verbose(
114+
"Current SSID: \(String(describing: hotspotNetwork?.ssid)), current BSSID: \(String(describing: hotspotNetwork?.bssid))"
115+
)
116+
let ssid = hotspotNetwork?.ssid
117+
self.currentWiFiSSID = {
118+
#if targetEnvironment(simulator)
119+
return "Simulator"
120+
#endif
121+
return ssid
122+
}
123+
let bssid = hotspotNetwork?.bssid
124+
self.currentWiFiBSSID = { bssid }
125+
}
126+
}
114127
}

0 commit comments

Comments
 (0)