iPhone13以上获取wifi名称2
1.应用的场景一般是:和硬件设备连接,需要软件获取WiFi名称,手动输入密码后,进行硬件配网操作。
1.进入开发者中心,在Identifiers下,在Capabilities里勾选Access WiFi Information。如下图:
2.xcode里添加获取WiFi信息的权限。如下图:
3.开启定位:iOS13以后,获取WiFi名称需要先开启定位
1) info.plist 文件需要配置获取的权限
Privacy - Location Always Usage Description
Privacy - Location When In Use Usage Description
4.ios13后
升级到iOS13以后,发现之前获取WiFi名称的接口失效了,返回的都是固定值"WLAN"。这里可能是因为苹果对用户隐私保护问题,因为通过wifi信息可以定位到用户地理位置。所以iOS13以后如果想要继续获取WiFi名称,需要在调用接口前判断用户是否同意app使用地理位置信息。
1.添加定位库
2.2.在Info.plist文件中配置,我把关于定位的4个都配上了
5.添加头文件
#import <CoreLocation/CoreLocation.h>
#import <NetworkExtension/NetworkExtension.h>
#import <CoreLocation/CLLocationManager.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/CaptiveNetwork.h>
6.添加定位管理代理
@interface ViewControllerAccount_setup : UIViewController<CBCentralManagerDelegate, CBPeripheralDelegate,CLLocationManagerDelegate>
7.添加CLLocationManager的变量
@property (strong, nonatomic) CLLocationManager *locationmanager;
#pragma mark - 定位授权代理方法
-
(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
status == kCLAuthorizationStatusAuthorizedAlways) {
//再重新获取ssid
[self getSSID];
}
} -
(void)getLocation
{
if (!self.locationmanager) {
self.locationmanager = [[CLLocationManager alloc] init];}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//这句代码会在app的设置中开启位置授权的选项,只有用户选择了允许一次,下次用户调用这个方法才会弹出询问框,选择不允许或是使用期间允许,下次调用这个方法都不会弹出询问框
[self.locationmanager requestAlwaysAuthorization];}
self.locationmanager.delegate = self;
//如果用户第一次拒绝了,弹出提示框,跳到设置界面,要用户打开位置权限
//如果用户跳到设置界面选择了下次询问,再回到app,[CLLocationManager authorizationStatus]的值会是nil,所以要||后面的判断
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied || ![CLLocationManager authorizationStatus]) {
[self alertMy];
}
} -
(void)alertMy{
//1.创建UIAlertControler
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@“提示” message:@“app需要获取您的位置权限,以获取wifi信息,给机器人配网” preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *conform = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //使用下面接口可以打开当前应用的设置页面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; NSLog(@"点击了确认按钮"); }]; //2.2 取消按钮 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了取消按钮"); }]; //3.将动作按钮 添加到控制器中 [alert addAction:conform]; [alert addAction:cancel]; //4.显示弹框 [self presentViewController:alert animated:YES completion:nil];
}
-
(void)getSSID{
NSString *ssidStr = [ViewControllerAccount_setup fetchSSIDInfo][@“SSID”];NSLog(@“ssidStr=%@”,ssidStr);
}
-
(id)fetchSSIDInfo {
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
id info = nil;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);if (info && [info count]) { break; }
}
return info;
}