This works if you run it from outside Xcode. If you run it in Xcode, won't work.
import SwiftUI
import CoreWLAN
import SystemConfiguration.CaptiveNetwork
import CoreLocation
class LocationDelegate: NSObject, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var onAuthorizationGranted: (() -> Void)?
override init() {
super.init()
locationManager.delegate = self
}
func requestLocation() {
locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .authorizedWhenInUse, .authorizedAlways:
onAuthorizationGranted?()
default:
break
}
}
}
struct ContentView: View {
@State private var currentBSSID: String = "Loading..."
let locationDelegate = LocationDelegate()
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Current BSSID:")
Text(currentBSSID)
}
.padding()
.onAppear {
locationDelegate.onAuthorizationGranted = {
fetchBSSID()
}
locationDelegate.requestLocation()
}
}
func fetchBSSID() {
if let iface2 = CWWiFiClient.shared().interface() {
print("✅ Found Wi-Fi interface: \(iface2.interfaceName ?? "nil")")
} else {
print("❌ No Wi-Fi interface found")
}
if let iface = CWWiFiClient.shared().interface(),
let bssid = iface.bssid() {
currentBSSID = bssid
} else {
currentBSSID = "Not connected"
print("✅ BSSID: \(currentBSSID)")
}
}
}
#Preview {
ContentView()
}
Topic:
App & System Services
SubTopic:
Networking
Tags: