iOS获取WIFI名称

2023-08-31  本文已影响0人  沉淀纷飞

在物联网领域,常会用到配网技术,其中就包含WIFI配网。在WIFI配网过程中,我们就需要拿到设备当前连接的WIFI连接信息。下面我们具体研究下一个iOS应用是如何获取WIFI的连接信息。

  1. 第一步需要在开发者账号Identifiers里面配置,相关应用拥有获取WIFI信息的权限
1.jpg
  1. 在xcode中修改相关配置
20190428143826889.png 截屏2023-08-30 14.35.51.png
  1. 在.m引入头文件 #import <SystemConfiguration/CaptiveNetwork.h> #import <CoreLocation/CoreLocation.h> #import <NetworkExtension/NetworkExtension.h>
  2. 系统低于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;
                    }
                }

            }

}

上一篇下一篇

猜你喜欢

热点阅读