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 {
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)")

View File

@ -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) {

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
#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 {