iOS 代码 获取wifi信息 及主动连接WiFi

兄弟们帮我讲解下,iOS 代码 获取wifi信息 及主动连接WiFi
最新回答
Cunese-红玫瑰*

2024-07-27 08:32:16

>

info.ssid = dic[@"SSID"]; 

 info.bssid = dic[@"BSSID"];

 info.ssidData = dic[@"SSIDDATA"]; 

 NSString *str = [[NSString alloc] initWithData:dic[@"SSIDDATA"] encoding:NSUTF8StringEncoding]; 

 NSLog(@"%@", str);

>

-(NSDictionary *)getWifiInfo{

    NSArray *ifs = (__bridge_transfer id)(CNCopySupportedInterfaces());

    //NSLog(@"interface %@", ifs);

    NSDictionary *info = nil;

    for (NSString *ifname in ifs) {

        info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);

        //NSLog(@"%@ => %@",  ifname, info);

    }

    return info;

}

>

2、主动连接
//
https://blog.csdn.net/github_28024665/article/details/78222471


#import < NetworkExtension/NEHotspotConfigurationManager.h  >

- (IBAction)connectWifi:(id)sender {

    //创建将要连接的WIFI配置实例

    NEHotspotConfiguration * hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:@"Xiaomi_9A36" passphrase:@"12121212" isWEP:NO];

    // 开始连接 (调用此方法后系统会自动弹窗确认)

    [[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) {

        NSLog(@"%@", error);

        if (!error) {

            self.msgLabel.text = @"切换成功";

        }else{

            self.msgLabel.text = error.localizedDescription;

        }

    }];

}