如何获取 iOS BSSID
How to Get iOS BSSID
大家好,我需要在 iPhone 和 iOS 12 和 SWIFT 上获取 wifi 网络的 BSSID。我测试了这样的东西
func getWiFiSsid() -> String? {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
return ssid}
我已经启用了访问 WiFi 信息功能,但仍然得到 NIL
感谢您的帮助
2018 年 11 月 26 日更新
这是我的ViewController.swift
import UIKit
import SystemConfiguration.CaptiveNetwork
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func getSSID() -> String? {
guard let interface = (CNCopySupportedInterfaces() as? [String])?.first,
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interface as CFString) as? [String: Any],
let ssid = unsafeInterfaceData["SSID"] as? String else{
return nil
}
return ssid
}
// Return IP address of WiFi interface (en0) as a String, or `nil`
func getWiFiAddress() -> String? {
var address : String?
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return nil }
guard let firstAddr = ifaddr else { return nil }
// For each interface ...
for ifptr in sequence(first: firstAddr, next: { [=12=].pointee.ifa_next }) {
let interface = ifptr.pointee
// Check for IPv4 or IPv6 interface:
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
// Check interface name:
let name = String(cString: interface.ifa_name)
if name == "en0" {
// Convert interface address to a human readable string:
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.pointee.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
return address
}
func getWiFiSsid() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
return ssid
}
func getSSIDtwo() -> String? {
let interfaces = CNCopySupportedInterfaces()
if interfaces == nil {
//print("Not interfaces")
return nil
}
let interfacesArray = interfaces as! [String]
if interfacesArray.count <= 0 {
return nil
}
let interfaceName = interfacesArray[0] as String
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName as CFString)
if unsafeInterfaceData == nil {
return nil
}
let interfaceData = unsafeInterfaceData as! Dictionary <String,AnyObject>
return interfaceData["SSID"] as? String
}
@IBAction func onButtonClick(_ sender: Any) {
let one = getWiFiSsid();
let two = getSSIDtwo();
let three = getSSID();
print(one);
print(two);
print(three);
}}
这是我在打开 Access WiFi 时使用的代码。您会看到我将数组中的第一项获取到并从中获取 SSID。
func getSSID() -> String? {
let interfaces = CNCopySupportedInterfaces()
if interfaces == nil {
//print("Not interfaces")
return nil
}
let interfacesArray = interfaces as! [String]
if interfacesArray.count <= 0 {
return nil
}
let interfaceName = interfacesArray[0] as String
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName as CFString)
if unsafeInterfaceData == nil {
return nil
}
let interfaceData = unsafeInterfaceData as! Dictionary <String,AnyObject>
return interfaceData["SSID"] as? String
}
当我使用更小更简洁的代码时,它也适用于我。请注意,我确实对您的代码做了两处更改:
func getWiFiSsid() -> String? {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
// Changed line below from the "ssid =" to return the value directly.
return interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
// Changed this to return nil
return nil
}
大家好,我需要在 iPhone 和 iOS 12 和 SWIFT 上获取 wifi 网络的 BSSID。我测试了这样的东西
func getWiFiSsid() -> String? {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
return ssid}
我已经启用了访问 WiFi 信息功能,但仍然得到 NIL
感谢您的帮助
2018 年 11 月 26 日更新
这是我的ViewController.swift
import UIKit
import SystemConfiguration.CaptiveNetwork
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func getSSID() -> String? {
guard let interface = (CNCopySupportedInterfaces() as? [String])?.first,
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interface as CFString) as? [String: Any],
let ssid = unsafeInterfaceData["SSID"] as? String else{
return nil
}
return ssid
}
// Return IP address of WiFi interface (en0) as a String, or `nil`
func getWiFiAddress() -> String? {
var address : String?
// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs>?
guard getifaddrs(&ifaddr) == 0 else { return nil }
guard let firstAddr = ifaddr else { return nil }
// For each interface ...
for ifptr in sequence(first: firstAddr, next: { [=12=].pointee.ifa_next }) {
let interface = ifptr.pointee
// Check for IPv4 or IPv6 interface:
let addrFamily = interface.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
// Check interface name:
let name = String(cString: interface.ifa_name)
if name == "en0" {
// Convert interface address to a human readable string:
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface.ifa_addr, socklen_t(interface.ifa_addr.pointee.sa_len),
&hostname, socklen_t(hostname.count),
nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
return address
}
func getWiFiSsid() -> String? {
var ssid: String?
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
return ssid
}
func getSSIDtwo() -> String? {
let interfaces = CNCopySupportedInterfaces()
if interfaces == nil {
//print("Not interfaces")
return nil
}
let interfacesArray = interfaces as! [String]
if interfacesArray.count <= 0 {
return nil
}
let interfaceName = interfacesArray[0] as String
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName as CFString)
if unsafeInterfaceData == nil {
return nil
}
let interfaceData = unsafeInterfaceData as! Dictionary <String,AnyObject>
return interfaceData["SSID"] as? String
}
@IBAction func onButtonClick(_ sender: Any) {
let one = getWiFiSsid();
let two = getSSIDtwo();
let three = getSSID();
print(one);
print(two);
print(three);
}}
这是我在打开 Access WiFi 时使用的代码。您会看到我将数组中的第一项获取到并从中获取 SSID。
func getSSID() -> String? {
let interfaces = CNCopySupportedInterfaces()
if interfaces == nil {
//print("Not interfaces")
return nil
}
let interfacesArray = interfaces as! [String]
if interfacesArray.count <= 0 {
return nil
}
let interfaceName = interfacesArray[0] as String
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName as CFString)
if unsafeInterfaceData == nil {
return nil
}
let interfaceData = unsafeInterfaceData as! Dictionary <String,AnyObject>
return interfaceData["SSID"] as? String
}
当我使用更小更简洁的代码时,它也适用于我。请注意,我确实对您的代码做了两处更改:
func getWiFiSsid() -> String? {
if let interfaces = CNCopySupportedInterfaces() as NSArray? {
for interface in interfaces {
if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
// Changed line below from the "ssid =" to return the value directly.
return interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
}
}
}
// Changed this to return nil
return nil
}