I'm trying to get the password of the current user's wifi. I'm trying the Security framework mentioned by @eskimo here, but it's not working. I'm trying the CLI command
public func getPassword(ssid: String) -> String? {
let command = "security find-generic-password -l \"\(ssid)\" -D 'AirPort network password' -w"
let result = shell(command)
return result
}
private func shell(_ command: String, lauchPath: String = "/bin/zsh") -> String? {
let task = Process()
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
task.arguments = ["-c", command]
task.launchPath = "/bin/zsh"
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .newlines)
return output
}
I'm now trying AppKit so I declared:
@IBOutlet var passwordLabel: NSTextField!
var password: String?
var wifi: String?
...
// applicationDidFinishLaunching(_ aNotification:)
wifi = CWWiFiClient.shared().interface()?.ssid()
....
password = getPassword(ssid: wifi ?? "Unknown")
passwordLabel.stringValue = password ?? "ERROR"
in applicationDidFinishLaunching(_ aNotification:)
it worked.
- Note: on my dialog, there's only
Deny
andAllow
, notDeny
,Allow Once
,Always Allow
I added a refresh button next to the password label and it won't work when I press the button:
@IBAction func RefreshPassword(_ sender: NSButton) {
wifi = CWWiFiClient.shared().interface()?.ssid()
password = getPassword(ssid: wifi)
print(password)
if let i = wifi {
DeviceWiFiLabel.stringValue = i
DeviceWiFiLabel.textColor = .systemGreen
passwordLabel.textColor = .textColor
} else {
DeviceWiFiLabel.stringValue = "None"
DeviceWiFiLabel.textColor = .systemOrange
passwordLabel.textColor = .red
}
passwordLabel.stringValue = password ?? "ERROR"
}
The dialog still pops open but I can't enter text when the textfield is focused, that is, no text appears when I hit a key when textfield focused. When I dismiss using the "Deny" button, sometimes a message like these appears in the console:
2022-08-24 05:21:24.034479+0800 MyApp[22457:564086] Detected potentially harmful notification post rate of <some sort of float-point number that always changes and about 300) notifications per second
I can't always reproduce the issue.
Any idea why?