美文网首页
iOS_获取设备当前连接的Wi-Fi信息

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

作者: 耀敬业 | 来源:发表于2017-12-01 12:33 被阅读0次

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

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

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

.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信息

      本文链接:https://www.haomeiwen.com/subject/vvnibxtx.html