Use NEHotspotNetwork to fetch current SSID

Old method did not work on Catalyst.
This commit is contained in:
Davide De Rosa 2022-05-15 22:16:27 +02:00
parent 743facca6b
commit 35ec73570e
3 changed files with 22 additions and 35 deletions

View File

@ -144,11 +144,11 @@ extension IntentDispatcher {
}
private static func handleCurrentNetwork(_ trust: Bool, _ vpnManager: VPNManager) {
guard let ssid = Utils.currentWifiNetworkName() else {
Task {
guard let ssid = await Utils.currentWifiSSID() else {
pp_log.warning("Not connected to any Wi-Fi or no permission to read location (needs 'While Using' or 'Always')")
return
}
Task {
do {
try await vpnManager.modifyActiveProfile {
pp_log.info("Wi-Fi SSID: \(ssid)")

View File

@ -51,10 +51,12 @@ public class SSIDReader: NSObject, ObservableObject, CLLocationManagerDelegate {
}
private func notifyCurrentSSID() {
let currentSSID = Utils.currentWifiNetworkName() ?? ""
Task {
let currentSSID = await Utils.currentWifiSSID() ?? ""
publisher.send(currentSSID)
cancellables.removeAll()
}
}
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {

View File

@ -24,11 +24,7 @@
//
import Foundation
#if os(iOS)
import SystemConfiguration.CaptiveNetwork
#else
import CoreWLAN
#endif
import NetworkExtension
extension Utils {
#if targetEnvironment(simulator)
@ -56,32 +52,21 @@ extension Utils {
}
#endif
public static func currentWifiSSID() async -> String? {
#if targetEnvironment(simulator)
public static func currentWifiNetworkName() -> String? {
// return nil
return ["My Home Network", "Safe Wi-Fi", "Friend's House"].randomElement()
}
["My Home Network", "Safe Wi-Fi", "Friend's House"].randomElement()
#else
public static func currentWifiNetworkName() -> String? {
#if os(iOS)
guard let interfaceNames = CNCopySupportedInterfaces() as? [CFString] else {
return nil
await withCheckedContinuation { continuation in
NEHotspotNetwork.fetchCurrent {
guard let network = $0 else {
continuation.resume(with: .success(nil))
return
}
for name in interfaceNames {
guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else {
continue
continuation.resume(with: .success(network.ssid))
}
guard let ssid = iface[kCNNetworkInfoKeySSID as String] as? String else {
continue
}
return ssid
}
return nil
#else
return CWWiFiClient.shared().interface()?.ssid()
#endif
}
#endif
}
extension Utils {