在物联网领域,常会用到配网技术,其中就包含WIFI配网。在WIFI配网过程中,我们就需要拿到设备当前连接的WIFI连接信息。下面我们具体研究下一个iOS应用是如何获取WIFI的连接信息。
- 第一步需要在开发者账号Identifiers里面配置,相关应用拥有获取WIFI信息的权限
- 在xcode中修改相关配置
- 你需要在Build Phase里面添加上SystemConfiguration.framework
- 在capableilities中打开wifi
- 工程的plist文件里面需要配置获取位置信息的隐私权限
- 在.m引入头文件 #import <SystemConfiguration/CaptiveNetwork.h> #import <CoreLocation/CoreLocation.h> #import <NetworkExtension/NetworkExtension.h>
- 系统低于iOS14以下和高于iOS14的获取WIFI的方法还有差异,还有一点需要注意获取WIFI之前需先获得位置权限,具体方法如下
//获取位置权限
self.locationmanager = [[CLLocationManager alloc]init];
self.locationmanager.delegate = self;
[self.locationmanager requestAlwaysAuthorization];
[self.locationmanager requestWhenInUseAuthorization];
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
{
NSLog(@"用户还未决定授权");
break;
}
case kCLAuthorizationStatusRestricted:
{
break;
}
case kCLAuthorizationStatusDenied:
{
// 类方法,判断是否开启定位服务
if (self.lblWifiName.text.length == 0) {
[self checkLocaltion];
}
break;
}
case kCLAuthorizationStatusAuthorizedAlways:
{
NSLog(@"获得前后台授权");
[self getWIFI];
break;
}
case kCLAuthorizationStatusAuthorizedWhenInUse:
{
NSLog(@"获得前台授权");
[self getWIFI];
break;
}
default:
break;
}
}
#pragma mark - 获取定位权限
- (void)checkLocaltion {
if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"获取Wi-Fi名称需要开启位置权限,是否前往开启?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:true];
}];
[alertController addAction:cancelAction];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}];
[alertController addAction:confirmAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
#pragma mark - 获取Wifi名称
- (void)getWIFI {
if (@available(iOS 14.0, *)) {
BOOL isFullAccuracy = weakSelf.locationmanager.accuracyAuthorization == CLAccuracyAuthorizationFullAccuracy;
if (!isFullAccuracy) {
// 向用户申请临时开启一次精确位置权限
[weakSelf.locationmanager requestTemporaryFullAccuracyAuthorizationWithPurposeKey:@"WantsToGetWiFiSSID"];
}
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {
NSString *wifiName = currentNetwork.SSID;
NSString *macName = currentNetwork.BSSID;
}];
} else {
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if(info && [info count]){
NSDictionary *dic = (NSDictionary*)info; //取得网卡咨询
NSLog(@"CNCopySupportedInterfaces info = %@",dic);
NSString *wifiName = [dic objectForKey:@"SSID"]; //取得ssid
NSString *beforeMac = [NSString stringWithFormat:@"%@",dic[@"BSSID"]];
NSString *macName = @"";
NSArray *macArray = [MBTools componentString:beforeMac withSymbol:@":"];
for (int i = 0; i < macArray.count; i ++) {
NSString *tem = macArray[i];
if (tem.length == 1) {
tem = [NSString stringWithFormat:@"0%@",tem];
}
macName = [NSString stringWithFormat:@"%@%@",macName,tem];
}
break;
}
}
}
}