iPhone13以上获取wifi名称

iPhone13以上获取wifi名称

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;

8.locationManager变量初始化,使用授权的申请

-(CLLocationManager*)locationManager{
if (!_locationmanager) {
_locationmanager = [[CLLocationManager alloc] init];
_locationmanager.delegate = self;
_locationmanager.distanceFilter = kCLLocationAccuracyThreeKilometers;
[_locationmanager requestWhenInUseAuthorization];
}
return _locationmanager;
}

在这里插入图片描述
8.开始定位调用
[self.locationManager startUpdatingLocation];

在这里插入图片描述
9.代理回调
@implementation ViewControllerAccount_setup
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
NSString *str_wifiname = [ViewControllerAccount_setup wifiName];
NSLog(@“str_wifiname=%@”,str_wifiname);
}
在这里插入图片描述
10.wifi名称获取

  • (NSString *)wifiName
    {
    NSArray *ifs = CFBridgingRelease(CNCopySupportedInterfaces());
    id info = nil;
    for (NSString *ifname in ifs) {
    info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((CFStringRef) ifname);
    if (info && [info count]) {
    break;
    }
    }
    NSDictionary *dic = (NSDictionary *)info;
    NSString *ssid = [[dic objectForKey:@“SSID”] lowercaseString];

    return ssid;
    }

在这里插入图片描述
至此大功告成。

猜你喜欢

转载自blog.csdn.net/wangyuhong2267/article/details/132880340