Get current WiFi password programmatically

Hello,

I'm trying to find a way, to programmatically get the password, of current WiFi connection, so I could use the password to connect another device to the same WiFi.

One of the possibilities that I've found was using a terminal command: "security find-generic-password -ga TypeInYourWiFiNameHere | grep “password:”".

Is there any other, more appropriate way to do this in Objective C/C++? Asking user for permission is completely fine and understandable.

Answered by DTS Engineer in 692201022

The Mac stores the Wi-Fi password in the System keychain. The best way to get it is via CoreWLAN’s CWKeychainFindWiFiPassword function. For example:

import CoreWLAN

func password(for ssid: String) throws -> String {
    let ssid = Data(ssid.utf8)
    var passwordQ: NSString? = nil
    let err = CWKeychainFindWiFiPassword(CWKeychainDomain.system, ssid, &passwordQ)
    guard err == noErr else {
        throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
    }
    return passwordQ! as String
}

This will prompt for an admin password. There’s no way to get this info without that prompt, for obvious security reasons.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm trying to find a way, to programmatically get the password, of current WiFi connection

On what platform?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm trying to do this for Mac.

Accepted Answer

The Mac stores the Wi-Fi password in the System keychain. The best way to get it is via CoreWLAN’s CWKeychainFindWiFiPassword function. For example:

import CoreWLAN

func password(for ssid: String) throws -> String {
    let ssid = Data(ssid.utf8)
    var passwordQ: NSString? = nil
    let err = CWKeychainFindWiFiPassword(CWKeychainDomain.system, ssid, &passwordQ)
    guard err == noErr else {
        throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
    }
    return passwordQ! as String
}

This will prompt for an admin password. There’s no way to get this info without that prompt, for obvious security reasons.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Is there a way to do this on iOS?

Not directly.

Why are you trying to do this? Most folks who ask this question are trying to perform some sort of accessory integration task, for example, configure a Wi-Fi based accessory to join the user’s Wi-Fi network. The recommended approach for that is WAC. See TN3111 iOS Wi-Fi API overview for links to more info.

If WAC isn’t applicable to your issue, you’ll have to ask the user for their Wi-Fi password.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Get current WiFi password programmatically
 
 
Q