获取当前连接的wifi的名字

先添加SystemConfiguration.framework库文件,并包含头文件

#import <SystemConfiguration/CaptiveNetwork.h>

- (NSString *)getWifiName

{

    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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用`Wlanapi`库中的`WlanQueryInterface`函数和`WlanGetProfile`函数来获取当前连接WiFi名称。具体步骤如下: 1. 首先使用`WlanOpenHandle`函数打开一个句柄。 2. 然后使用`WlanEnumInterfaces`函数获取当前计算机的无线网络接口。 3. 使用`WlanQueryInterface`函数查询接口状态,判断是否连接WiFi网络。 4. 如果连接WiFi网络,则调用`WlanGetProfile`函数获取当前连接WiFi名称。 以下是示例代码: ``` #include <Windows.h> #include <Wlanapi.h> #include <iostream> #pragma comment(lib, "Wlanapi.lib") int main() { DWORD dwMaxClient = 2; // 最大客户端数量 HANDLE hClient = NULL; PWLAN_INTERFACE_INFO_LIST pIfList = NULL; PWLAN_INTERFACE_INFO pIfInfo = NULL; DWORD dwRetVal = 0; DWORD dwInterfaceIndex = 0; WLAN_INTERFACE_STATE wlanInterfaceState = WlanInterfaceStateNotReady; WCHAR profileName[256]; DWORD pdwDataSize = sizeof(profileName); DWORD dwFlags = 0; // 打开句柄 dwRetVal = WlanOpenHandle(dwMaxClient, NULL, &dwFlags, &hClient); if (dwRetVal != ERROR_SUCCESS) { std::cerr << "WlanOpenHandle failed with error: " << dwRetVal << std::endl; return 1; } // 枚举接口 dwRetVal = WlanEnumInterfaces(hClient, NULL, &pIfList); if (dwRetVal != ERROR_SUCCESS) { std::cerr << "WlanEnumInterfaces failed with error: " << dwRetVal << std::endl; WlanCloseHandle(hClient, NULL); return 1; } // 遍历接口 for (DWORD i = 0; i < pIfList->dwNumberOfItems; i++) { pIfInfo = &pIfList->InterfaceInfo[i]; // 查询接口状态 dwRetVal = WlanQueryInterface(hClient, &pIfInfo->InterfaceGuid, wlan_intf_opcode_interface_state, NULL, &pdwDataSize, (PVOID*)&wlanInterfaceState, NULL); if (dwRetVal != ERROR_SUCCESS) { std::cerr << "WlanQueryInterface failed with error: " << dwRetVal << std::endl; WlanFreeMemory(pIfList); WlanCloseHandle(hClient, NULL); return 1; } // 如果接口连接WiFi网络,则获取WiFi名称 if (wlanInterfaceState == WlanInterfaceStateConnected) { dwRetVal = WlanGetProfile(hClient, &pIfInfo->InterfaceGuid, NULL, profileName, &pdwDataSize, NULL, &dwFlags); if (dwRetVal != ERROR_SUCCESS) { std::cerr << "WlanGetProfile failed with error: " << dwRetVal << std::endl; WlanFreeMemory(pIfList); WlanCloseHandle(hClient, NULL); return 1; } std::wcout << "Current WiFi name is: " << profileName << std::endl; break; } } WlanFreeMemory(pIfList); WlanCloseHandle(hClient, NULL); return 0; } ``` 这段代码会输出当前连接WiFi名称。注意,由于使用了`Wlanapi`库,需要在编译时链接`Wlanapi.lib`库文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值