l3aronsansgland
l3aronsansgland

Reputation: 344

Getting list of preferred wifi networks on Mac with Swift 3

Is there a way to get the list of preferred ( = saved) wifi's ssid on MacOS with Swift 3.0 ?

I found some example which are deprecated and are (surprisingly) only runnable on iOS.

Network Preferences

Upvotes: 1

Views: 735

Answers (2)

l3aronsansgland
l3aronsansgland

Reputation: 344

It might not be the most beautiful code ever, but it works in Swift 3.0.

func shell(arguments: [String] = []) -> (String? , Int32) {
    let task = Process()
    task.launchPath = "/bin/bash"
    task.arguments = arguments

    let pipe = Pipe()
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = String(data: data, encoding: .utf8)
    let terminationStatus = task.terminationStatus
    return (output, terminationStatus)
}

Extensions:

extension String {
    func stringByReplacingFirstOccurrenceOfString(
        target: String, withString replaceString: String) -> String
    {
        if let range = self.range(of: target) {
            return self.replacingCharacters(in: range, with: replaceString)
        }
        return self
    }
}

extension String {
    func stringByReplacingLastOccurrenceOfString(
        target: String, withString replaceString: String) -> String
    {
        if let range = self.range(of: target, options: String.CompareOptions.backwards) {
            return self.replacingCharacters(in: range, with: replaceString)
        }
        return self
    }
}

Get and clean the wifi's SSIDs

let (output, terminationStatus) = shell(arguments: ["-c", "defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences | grep SSIDString"])
    if (terminationStatus == 0) {
        let arrayOfWifi = output?.components(separatedBy: CharacterSet.newlines)

        for var aWifi in arrayOfWifi! {
            aWifi = aWifi.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            if (aWifi.hasPrefix("SSIDString = ")) {
                aWifi = aWifi.stringByReplacingFirstOccurrenceOfString(target: "SSIDString = ", withString: "")
            }
            if (aWifi.hasPrefix("\"")) {
                aWifi = aWifi.stringByReplacingFirstOccurrenceOfString(target: "\"", withString: "")
            }
            if (aWifi.hasSuffix("\";")) {
                aWifi = aWifi.stringByReplacingLastOccurrenceOfString(target: "\";", withString: "")
            }
            if (aWifi.hasSuffix(";")) {
                aWifi = aWifi.stringByReplacingLastOccurrenceOfString(target: ";", withString: "")
            }
            aWifi = aWifi.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
            print(aWifi)
        }
    }
}

Upvotes: 0

JAL
JAL

Reputation: 42449

Preferred Networks are stored in a plist as part of System Preferences NSUserDefaults. While I don't see an API to access these names directly, you can use the defaults shell command or NSTask to access the values:

defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences | grep SSIDString

Note that in this list are not only all of the SSIDs that the computer has connected to, but the list synced with any iCloud-enabled device.

Related discussion here: OS X Daily - See a List of All Wi-Fi Networks a Mac Has Previously Connected To

Upvotes: 1

Related Questions