I have this code:
import SwiftUI
import NetworkExtension
import CoreLocation
class WifiDataViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {
@Published var ssid: String = ""
@Published var signalStrength: Double = 0
private let locationManager = CLLocationManager()
override init() {
super.init()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
func fetchWifiData() {
NEHotspotNetwork.fetchCurrent { network in
guard let network else {
print("No available network")
return
}
self.ssid = network.ssid
self.signalStrength = network.signalStrength
}
}
// CLLocationManagerDelegate methods
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse || status == .authorizedAlways {
fetchWifiData()
}
}
}
struct ContentView: View {
@StateObject private var viewModel = WifiDataViewModel()
var body: some View {
VStack {
Text("Wi-Fi Data")
.font(.title)
.padding()
Text("Network SSID: \(viewModel.ssid)")
.font(.subheadline)
Text("Signal Strength: \(viewModel.signalStrength) dBm")
.font(.subheadline)
Button(action: {
viewModel.fetchWifiData()
}) {
Text("Fetch Wi-Fi Data")
.font(.headline)
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
.padding()
}
}
The goal of this app is to find a weak spot in my home network, to manage router placement better.
I want to get wifi signal strength in any possible way, either it will be rssi value or [0, 1] value.
Documentation tells that signalStrength should provide such value, but it isn't. https://developer.apple.com/documentation/networkextension/nehotspotnetwork/1618923-signalstrength
From additional setup, Access Wi-Fi Information capability is granted.
iOS 16.4, Swift
please help me resolving this issue