Skip to content

Instantly share code, notes, and snippets.

@GreenGeorge
Created March 29, 2019 06:50
Show Gist options
  • Save GreenGeorge/388f76970b7577ac227dbc4aacf2c836 to your computer and use it in GitHub Desktop.
Save GreenGeorge/388f76970b7577ac227dbc4aacf2c836 to your computer and use it in GitHub Desktop.
Swift network identifier class
//
// NetworkIdentifier.swift
// networkIdentifier
//
// Created by George Ananda on 29/03/19.
// Copyright © 2019 George Ananda. All rights reserved.
//
import Foundation
import SystemConfiguration.CaptiveNetwork
struct NetworkInfo {
let interface: String
let ssid: String
let bssid: String
}
class NetworkIdentifier {
static var main = NetworkIdentifier()
private init() {
}
func getNetworkInfos() -> [NetworkInfo] {
guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
return []
}
let networkInfos: [NetworkInfo] = interfaceNames.compactMap {
name in
guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String: AnyObject] else {
return nil
}
guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
return nil
}
guard let bssid = info[kCNNetworkInfoKeyBSSID as String] as? String else {
return nil
}
return NetworkInfo(interface: name, ssid: ssid, bssid: bssid)
}
return networkInfos
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment