官方的API没有提供获取扫描所有wifi列表,相近功能的只有CaptiveNetwork,获取当前wifi的名称。
引用头文件<SystemConfiguration/CaptiveNetwork.h>
/*! @function CNCopySupportedInterfaces @discussion copies a list of all interfaces CaptiveNetworkSupport is monitoring. @result An array of CFStringRef- BSD interface names. Returns NULL if an error was encountered. You MUST release the returned value. */CFArrayRefCNCopySupportedInterfaces (void) __OSX_AVAILABLE_STARTING(__MAC_10_8,__IPHONE_4_1);
通过CNCopySupportedInterfaces获取wifi列表,实际测试中返回数组中只有一个值,即当前连接的wifi。
- (NSString *)currentWifiSSID
{
NSString *ssid = nil;
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
for (NSString *ifname in ifs)
{
NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
if (info[@"SSID"])
{
ssid = info[@"SSID"];
}
}
return ssid;
}