Created
March 29, 2019 06:50
-
-
Save GreenGeorge/388f76970b7577ac227dbc4aacf2c836 to your computer and use it in GitHub Desktop.
Swift network identifier class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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