iOS_获取设备当前连接的Wi-Fi信息

手机和智能设备匹配Wi-Fi时,我们需要获取手机当前连接的Wi-Fi信息,如SSID,BSSID等。

SSID:全称 Server Set IDentifier, 即Wi-Fi公开的名称
BSSIS:指站点的Mac地址

获取Wi-Fi信息的方法我整理为一个工具类,需要注意的是.h文件必须倒入头文件#import

.h文件

//SSID
+(NSString *)getDeviceConnectWifiName;

//BSSID
+(NSString *)getDeviceConnectWifiAddress;

//SSIDDATA
+(NSString *)getDeviceConnectWifiData;

.m文件

+(NSString *)getDeviceConnectWifiName{
    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam));
        if (info && [info count]) {
            break;
        }
    }
    if ([info isKindOfClass:[NSDictionary class]]) {
        //获取SSID
        return [info objectForKey:@"SSID"]; 
    }
    return nil;
}
+(NSString *)getDeviceConnectWifiAddress{
    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam));
        if (info && [info count]) {
            break;
        }
    }
    if ([info isKindOfClass:[NSDictionary class]]) {
        //获取BSSID
        return [info objectForKey:@"BSSID"];
    }
    return nil;
}
+(NSString *)getDeviceConnectWifiData{
    NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
    id info = nil;
    for (NSString *ifnam in ifs) {
        info = (__bridge id)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam));
        if (info && [info count]) {
            break;
        }
    }
    if ([info isKindOfClass:[NSDictionary class]]) {
        //获取SSIDDATA
        return [info objectForKey:@"SSIDDATA"];
    }
    return nil;
}

如此获取Wi-Fi信息基本可以满足要求了。

你可能感兴趣的:(iOS_获取设备当前连接的Wi-Fi信息)