获取手机SSID和BSSID以及IP地址
import SystemConfiguration
import SystemConfiguration.CaptiveNetwork
extension NSObject{
func GetIPAddresses() -> String? {
var addresses = String
var ifaddr : UnsafeMutablePointer
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while (ptr != nil) {
let flags = Int32(ptr!.pointee.ifa_flags)
var addr = ptr!.pointee.ifa_addr.pointee
if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {
var hostname = CChar)
if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),nil, socklen_t(0), NI_NUMERICHOST) == 0) {
if let address = String(validatingUTF8:hostname) {
addresses.append(address)
}
}
}
}
ptr = ptr!.pointee.ifa_next
}
freeifaddrs(ifaddr)
}
return addresses.first
}
func getUsedSSID() -> (String,String) {
let interfaces = CNCopySupportedInterfaces()
var ssid = “”
var bssid = “”
if interfaces != nil {
let interfacesArray = CFBridgingRetain(interfaces) as! Array
if interfacesArray.count > 0 {
let interfaceName = interfacesArray[0] as! CFString
let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
if (ussafeInterfaceData != nil) {
let interfaceData = ussafeInterfaceData as! Dictionary
ssid = interfaceData[“SSID”]! as! String
bssid = interfaceData[“BSSID”]! as! String
}
}
}
return (ssid,bssid)
}
}