Display all available WIFI connections with Swift in OS X

后端 未结 1 940
南旧
南旧 2021-01-03 16:15

I\'m trying to display all available WIFI connections. It doesn\'t work. Here is my code:

import Foundation
import CoreWLAN

var cwInterface = CWInterface()         


        
1条回答
  •  一生所求
    2021-01-03 16:46

    It works if you initialize CWInterface with an interface name, like "en1".

    But it's better to not use harcoded names, so we'll also use CWWiFiClient.sharedWiFiClient().interface() which returns the default WIFI interface.

    Example of a class to manage all this:

    class Discovery {
    
        var currentInterface: CWInterface
        var interfacesNames: [String] = []
        var networks: Set = []
    
        // Failable init using default interface
        init?() {
            if let defaultInterface = CWWiFiClient.sharedWiFiClient().interface(),
                   name = defaultInterface.interfaceName {
                self.currentInterface = defaultInterface
                self.interfacesNames.append(name)
                self.findNetworks()
            } else {
                return nil
            }
        }
    
        // Init with the literal interface name, like "en1"
        init(interfaceWithName name: String) {
            self.currentInterface = CWInterface(interfaceName: name)
            self.interfacesNames.append(name)
            self.findNetworks()
        }
    
        // Fetch detectable WIFI networks
       func findNetworks() {
            do {
                self.networks = try currentInterface.scanForNetworksWithSSID(nil)
            } catch let error as NSError {
                print("Error: \(error.localizedDescription)")
            }
        }
    
    }
    

    Call it with the default interface:

    if let discovery = Discovery() {
        print(discovery.networks)
        for network in discovery.networks {
            print(network.ssid!)
        }
    }
    

    Or with an interface name:

    let discovery = Discovery(interfaceWithName: "en1")
    let results = discovery.networks
    

    Results contains all the scanned networks:

    [ [ssid=SomeNetworkName, bssid=xxxx, security=WPA Enterprise, rssi=xx, channel= [channelNumber=11(2GHz), channelWidth={20MHz}], ibss=0], etc]

    0 讨论(0)
提交回复
热议问题