func getCurrentWifiName() -> String? {
var wifiName : String = ""
let wifiInterfaces = CNCopySupportedInterfaces()
if wifiInterfaces == nil {
return nil
}
let interfaceArr = CFBridgingRetain(wifiInterfaces!) as! Array<String>
if interfaceArr.count > 0 {
let interfaceName = interfaceArr[0] as CFString
let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
if (ussafeInterfaceData != nil) {
let interfaceData = ussafeInterfaceData as! Dictionary<String, Any>
wifiName = interfaceData["SSID"]! as! String
}
}
return wifiName
}
附上OC版本
//获取当前wifi名字
- (NSString *)getCurrentConnectWifiName
{
NSString *wifiName = nil;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
return nil;
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
NSLog(@"network info -> %@", networkInfo);
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
CFRelease(dictRef);
}
}
CFRelease(wifiInterfaces);
return wifiName;
}
网友评论