iOS 获取Wi-Fi列表名称

iOS 12之前:

id info = nil;
    NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
    for (NSString *ifnam in ifs) {
        info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
        NSString *str = info[@"SSID"];//name
        
    }

iOS 12之后以上方法获取不到,需要在Xcode中TARGET-->Capabilities打开Access WiFi Information选项


1A9BB7FD-1FC9-4EBA-BD0A-7438F3F86624.png

iOS 13之后以上方法获取Wi-Fi名返回的都是固定值"WLAN",这里可能是因为苹果保护用户隐私而产生的问题,因为通过wifi信息可以定位到用户地理位置。所以iOS13以后如果想要继续获取WiFi名称,需要在调用接口前判断用户是否同意App使用地理位置信息。可以在程序一启动时请求用户权限,调用的方法如下:

#import <CoreLocation/CoreLocation.h>

@property (strong, nonatomic) CLLocationManager *locationManager;

NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
CGFloat version = [phoneVersion floatValue];
// 如果是iOS13 未开启地理位置权限 需要提示一下
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined && version >= 13) {
    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager requestWhenInUseAuthorization];
}

如果用户拒绝了授权,在需要获取Wi-Fi名的界面加上提示:

    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    CGFloat version = [phoneVersion floatValue];
    //如果开启地理位置权限未开启 需要提示一下
    if (([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied  )&& version >= 13) {
        [PracticalTools showAlertViewWithTitle:@"提示" message:@"您的位置权限尚未授权,将无法获取当前Wi-Fi进行配置网络,请前往“设置”-“Photoegg”-“位置”进行授权!" doneText:@"确定" cancelText:nil doneHandle:nil cancelHandle:nil vc:self];
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容