Display All Available Wifi Connections with Swift in Os X

Display all available WIFI connections with Swift in OS X

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<CWNetwork> = []

// 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:

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

How can I retrieve list of Wi-Fi hotspots in Mac application?

After some more googling and reading forums I realized what caused my Swift code to crash the app and I fixed the bug, here's working example:

var cwInterface: CWInterface = CWInterface(name: "en1")
var netArray:[CWNetwork]=[]
var testArray:[String]=[]
netArray = Array(cwInterface.scanForNetworksWithName(nil , error: nil)) as! [CWNetwork]
var network=netArray[0]
for network in netArray
{
testArray.append(network.ssid)
}

iOS Get list of all WiFi networks

This might be possible with NetworkExtension (available since iOS 8). But you need the com.apple.developer.networking.HotspotHelper-entitlement in you app to use these APIs. To get these entitlement, you have to contact Apple and describe, why you need it.

I haven't found any code examples, but maybe the documentation of NetworkExtension helps you.

Retrieving WiFi network in Swift 3

I used the code below to get the current SSID, after checking for internet connectivity.

import CoreWLAN

let wifiNetwork = CWWiFiClient.shared().interface()!.ssid()!

How to get all previously connected Wi-Fi networks in Swift?

From a stock iPhone this is not possible from the settings. The information that you want is stored in the iPhone's keychain. If you have access to the iPhone SDK you can start messing around if you like, but I will leave it at that since that would be a StackOverflow answer. Source

If you have ICloud Keychain enabled, it is possible to fetch this data. Otherwise, you can't. Source You can use kishikawakatsumi/KeychainAccess or do it yourself. Here is homemade code to save and retrieve keychain values.
It is a simple swift wrapper for Keychain that works on iOS and OS X

How to display a list of Wifi-Networks in an iPhone app?

If you set the UIRequiresPersistentWiFi setting in your info.plist file, the iPhone OS will know that your app needs Wifi and pop up the message for you.

As far as I know, no app actually manually displays the wifi selection alert.



Related Topics



Leave a reply



Submit