iOS获取手机WiFi名称

应用的场景一般是:和硬件设备连接,需要软件获取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

2)开启定位代码

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

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    self.wifiTF.text = [NSObject wifiName];
}

4.获取WiFi名称

头文件
#import 
#import 
#import 
//封装的方法
+ (NSString *)wifiName{
    NSString *wifiName = @"Not Found";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil) {
        CFDictionaryRef myDict =CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict != nil) {
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict);
            wifiName = [dict valueForKey:@"SSID"];
        }
    }
    return wifiName;
}

大功告成!!!

你可能感兴趣的:(iOS获取手机WiFi名称)