From 35ec73570ed8304b685fdb706e4171eb5495d8a9 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 15 May 2022 22:16:27 +0200 Subject: [PATCH] Use NEHotspotNetwork to fetch current SSID Old method did not work on Catalyst. --- .../Intents/IntentDispatcher+Activities.swift | 8 ++-- .../Reusable/SSIDReader.swift | 8 ++-- .../Utils/Utils+Network.swift | 41 ++++++------------- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/Passepartout/App/Intents/IntentDispatcher+Activities.swift b/Passepartout/App/Intents/IntentDispatcher+Activities.swift index 53d994d1..209603bd 100644 --- a/Passepartout/App/Intents/IntentDispatcher+Activities.swift +++ b/Passepartout/App/Intents/IntentDispatcher+Activities.swift @@ -144,11 +144,11 @@ extension IntentDispatcher { } private static func handleCurrentNetwork(_ trust: Bool, _ vpnManager: VPNManager) { - guard let ssid = Utils.currentWifiNetworkName() else { - pp_log.warning("Not connected to any Wi-Fi or no permission to read location (needs 'While Using' or 'Always')") - return - } 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 + } do { try await vpnManager.modifyActiveProfile { pp_log.info("Wi-Fi SSID: \(ssid)") diff --git a/PassepartoutCore/Sources/PassepartoutUtils/Reusable/SSIDReader.swift b/PassepartoutCore/Sources/PassepartoutUtils/Reusable/SSIDReader.swift index 4e45d926..1e12c546 100644 --- a/PassepartoutCore/Sources/PassepartoutUtils/Reusable/SSIDReader.swift +++ b/PassepartoutCore/Sources/PassepartoutUtils/Reusable/SSIDReader.swift @@ -51,9 +51,11 @@ public class SSIDReader: NSObject, ObservableObject, CLLocationManagerDelegate { } private func notifyCurrentSSID() { - let currentSSID = Utils.currentWifiNetworkName() ?? "" - publisher.send(currentSSID) - cancellables.removeAll() + Task { + let currentSSID = await Utils.currentWifiSSID() ?? "" + publisher.send(currentSSID) + cancellables.removeAll() + } } public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) { diff --git a/PassepartoutCore/Sources/PassepartoutUtils/Utils/Utils+Network.swift b/PassepartoutCore/Sources/PassepartoutUtils/Utils/Utils+Network.swift index 19b15496..e73ed2b8 100644 --- a/PassepartoutCore/Sources/PassepartoutUtils/Utils/Utils+Network.swift +++ b/PassepartoutCore/Sources/PassepartoutUtils/Utils/Utils+Network.swift @@ -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 - #if targetEnvironment(simulator) - public static func currentWifiNetworkName() -> String? { -// return nil - return ["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 - } - for name in interfaceNames { - guard let iface = CNCopyCurrentNetworkInfo(name) as? [String: Any] else { - continue - } - guard let ssid = iface[kCNNetworkInfoKeySSID as String] as? String else { - continue - } - return ssid - } - return nil + public static func currentWifiSSID() async -> String? { + #if targetEnvironment(simulator) + ["My Home Network", "Safe Wi-Fi", "Friend's House"].randomElement() #else - return CWWiFiClient.shared().interface()?.ssid() + await withCheckedContinuation { continuation in + NEHotspotNetwork.fetchCurrent { + guard let network = $0 else { + continuation.resume(with: .success(nil)) + return + } + continuation.resume(with: .success(network.ssid)) + } + } #endif } - #endif } extension Utils {