How can I get the SSID of the wifi network I’m connected to?
I tried the method via the system_profiler command but it is very slow (2-3 seconds on a Mac M1)
The example of the CWWiFiClient class from MBS “/MacFrameworks/CWWiFiClient” returns the following errors:
StarMonitoring failed with type 10: The operation could not be completed/ (com.apple.wifi.request.error error 4.)
and
StarMonitoring failed with type 9: The operation could not be completed/ (com.apple.wifi.request.error error 4.)
Does anyone have a solution? (reading a plist file, for example…)
Thanks
Dan
How do I see this? (I don’t recognize this screen capture…)
For the record, I tried this, too, without success (no error but no string returned)
Try
Dim wifiClient As New CWWiFiClientMBS
Dim names() As String = wifiClient.interfaceNames
If names.Ubound < 0 Then
Return "Aucune interface Wi-Fi trouvée"
End If
For Each interfaceName As String In names
Dim wifiInterface As CWInterfaceMBS = wifiClient.interfaceWithName(interfaceName)
If wifiInterface <> Nil And wifiInterface.ssid <> "" Then
Return wifiInterface.ssid
End If
Next
Return "Pas de SSID actif trouvé"
Catch e As RuntimeException
Return "Erreur : " + e.Message
End Try
Thanks
The screenshot is from Xcode just as a hint, that I found it there in the entitlements editor.
You may edit the entitlement file yourself.
See there:
Following this link, it seems that this page does not concern macOS (but iOS
iPadOS and visionOS) and the CNCopyCurrentNetworkInfo command is deprecated by Apple
(CNCopyCurrentNetworkInfo | Apple Developer Documentation)
As a result, I can’t quite figure out how to run the CWWiFiClient example and get the SSID of my Wi-Fi network…
for exemple, Is it useful (essential?) to add permissions to the Info.plist file?
like:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application needs access to location information to identify the WiFi network.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This application needs access to location information to identify the WiFi network.</string>
Thank you for your insights,
Dan
This returns a list of all available SSIDs, but doesn’t tell you which you’re connected to, in case that helps.
Dim sh As New Shell, SSID As String
sh.ExecuteMode = shell.ExecuteModes.Synchronous
#If TargetWindows Then
sh.Execute("netsh wlan show networks")
If sh.ExitCode = 0 Then
SSID = sh.Result
End
#Else
Dim CWWiFiClient As New CWWiFiClientMBS
Dim CWI() As CWInterfaceMBS = CWWiFiClient.interfaces
Dim networks() As CWNetworkMBS = CWI(0).CachedScanResults
For Each n As CWNetworkMBS In Networks
SSID = SSID+n.ssid+EndOfLine
Next
#EndIf
@Denis_DUBOIS do you use a grep after system_profiler ?
because if you filter in xojo after receiving the whole system_profiler “talk” it can take a while
use awk or grep to extract the needed data in the command line.
edit: found this :
- Apple deprecated
airport
,networksetup -getairportnetwork
, and evenwdutil
for live SSID queries in macOS 15. - As of Sequoia,
SPAirPortDataType
is the only built-in source that always returns the current SSID without extra privileges, so the above patterns are now the recommended way for shell scripts and automation.
Merci Jean-Yves,
J’utilise cette bien cette methode :
Var sh As New Shell
sh.ExecuteMode = Shell.ExecuteModes.Synchronous
// Commande pour extraire le SSID actuel de system_profiler
Var command As String = "system_profiler SPAirPortDataType | " + _
"sed -n '/Current Network Information:/,/Other Local Wi-Fi Networks:/p' | " + _
"grep -E '^ *[A-Za-z0-9_-]+:$' | head -1 | sed 's/:$//' | sed 's/^ *//'"
sh.Execute(command)
Thanks for this interesting work, but my MessageBox(SSID) only returns empty strings!
I need to find out how to obtain localization rights to be able to read SSIDs and I haven’t found any clear explanations on the subject.
Dan
And that does not works as is (last Sequoia)…
Strange because it works for me under Sequoia 15.5
Try
Var sh As New Shell
sh.ExecuteMode = Shell.ExecuteModes.Synchronous
// Commande pour extraire le SSID actuel de system_profiler
Var command As String = "system_profiler SPAirPortDataType | " + _
"sed -n '/Current Network Information:/,/Other Local Wi-Fi Networks:/p' | " + _
"grep -E '^ *[A-Za-z0-9_-]+:$' | head -1 | sed 's/:$//' | sed 's/^ *//'"
sh.Execute(command)
If sh.ErrorCode = 0 Then
Return sh.Result.Trim
Else
Return "Erreur Shell : " + sh.ErrorCode.ToString
End If
Catch e As RuntimeException
Return "Erreur : " + e.Message
End Try