iOS13 无法获取WIFI(SSID)名字的问题

近期在测试公司 APP在iOS13上面的运行情况,发现一个问题,就是实际已经在设置里面连接了WIFI,但是在搜索WIFI名字的时候,搜到的SSID是空的。
查了一通资料,原来iOS13在获取WIFI名的时候,必须先取得定位权限,否则就是nil了。所以给出了如下解决方案。


WWDC19-CNCopyCurrentNetworkInfo().png

1.在需要获取WIFI名的地方导入框架

#import 

2.设置CLLocationManager,并设置相关Delegate "CLLocationManagerDelegate"

@property (nonatomic, strong) CLLocationManager *locationMagager;

- (CLLocationManager *)locationMagager {
    if (!_locationMagager) {
        _locationMagager = [[CLLocationManager alloc] init];
        _locationMagager.delegate = self;
    }
    return _locationMagager;
}

3.判断系统版本,做不同处理

if (@available(iOS 13, *)) {
        
        if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {//开启了权限,直接搜索
            
            [self searchWIFIName];
            
        } else if (CLLocationManager.authorizationStatus == kCLAuthorizationStatusDenied) {//如果用户没给权限,则提示
            
            [UIAlertController showAlertInViewController:self withTitle:@"定位权限关闭提示" message:@"你关闭了定位权限,导致无法使用WIFI功能" cancelButtonTitle:@"确定" destructiveButtonTitle:nil otherButtonTitles:nil tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {

            }];
            
        } else {//请求权限
            
            [self.locationMagager requestWhenInUseAuthorization];
        }
        
    } else {
        
        [self searchWIFIName];
    }

4.delegate回调

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    
    if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self searchWIFIName];
    }
}

这样就可以解决iOS13无法获取WIFI名字的问题了

你可能感兴趣的:(iOS13 无法获取WIFI(SSID)名字的问题)