passepartout-apple/PassepartoutLibrary/Sources/PassepartoutUtils/Reusable/SSIDReader.swift

75 lines
2.3 KiB
Swift
Raw Normal View History

2022-04-12 13:09:14 +00:00
//
// SSIDReader.swift
// Passepartout
//
// Created by Davide De Rosa on 2/24/22.
2023-03-17 15:56:19 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
2022-04-12 13:09:14 +00:00
//
// https://github.com/passepartoutvpn
//
// This file is part of Passepartout.
//
// Passepartout is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Passepartout is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import CoreLocation
@MainActor
public class SSIDReader: NSObject, ObservableObject {
2022-04-12 13:09:14 +00:00
private let manager = CLLocationManager()
2023-03-17 20:55:47 +00:00
private var continuation: CheckedContinuation<String, Error>?
2023-03-17 20:55:47 +00:00
private func currentSSID() async -> String {
await Utils.currentWifiSSID() ?? ""
}
2023-03-17 20:55:47 +00:00
public func requestCurrentSSID() async throws -> String {
2022-04-12 13:09:14 +00:00
switch manager.authorizationStatus {
case .authorizedAlways, .authorizedWhenInUse, .denied:
return await currentSSID()
2023-03-17 20:55:47 +00:00
2022-04-12 13:09:14 +00:00
default:
return try await withCheckedThrowingContinuation { continuation in
self.continuation = continuation
2023-03-17 20:55:47 +00:00
manager.delegate = self
manager.requestWhenInUseAuthorization()
}
}
2022-04-12 13:09:14 +00:00
}
}
2022-04-12 13:09:14 +00:00
extension SSIDReader: CLLocationManagerDelegate {
2022-04-12 13:09:14 +00:00
public func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedWhenInUse, .authorizedAlways, .denied:
Task {
continuation?.resume(returning: await currentSSID())
continuation = nil
}
2022-04-12 13:09:14 +00:00
default:
continuation?.resume(with: .success(""))
continuation = nil
2022-04-12 13:09:14 +00:00
}
}
2023-03-17 20:55:47 +00:00
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
continuation?.resume(throwing: error)
continuation = nil
}
2022-04-12 13:09:14 +00:00
}