1、获取WiFi信息
>
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;
}
}];
}