开发APP一般都会用到设备配网,获取当前设备的Wi-Fi 的SSID。那么如何获取呢?下面直接上代码:
第一步: 添加SystemConfiguration.framework这个框架
第二步:头文件 //导入框架头文件 #import <SystemConfiguration/CaptiveNetwork.h>
#pragma 获取设备当前连接的WIFI的SSID
- (NSString *) getCurrentWifi
{
NSString * wifiName = @"";
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
wifiName = @"";
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
CFRelease(dictRef);
}
}
//CFRelease(wifiInterfaces);
return wifiName;
}
#import <ifaddrs.h>
#import <arpa/inet.h>
#pragma mark - 获取当前Wi-Fi的IP
- (NSString *)getIPAddress {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
return address;
}
升级到iOS13以后,发现之前获取WiFi名称的接口失效了,返回的都是固定值"WLAN"。这里可能是因为苹果对用户隐私保护问题,因为通过wifi信息可以定位到用户地理位置。所以iOS13以后如果想要继续获取WiFi名称,需要在调用接口前判断用户是否同意app使用地理位置信息。
步骤一:添加定位库
步骤二:添加位置权限 在Info.plist文件中配置
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>移动考勤需要使用您的位置信息</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>移动考勤需要使用您的位置信息</string>
<key>NSLocationUsageDescription</key>
<string>移动考勤需要使用您的位置信息</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>移动考勤需要使用您的位置信息</string>
步骤三:在控制中引入头文件,遵守代理
#import <CoreLocation/CoreLocation.h>
@interface EspViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
步骤四:实现代理获取用户的定位授权重新获取SSID
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1]];
if(@available(iOS 13.0, *)){
[self getUserLocation];
}
}
#pragma mark - 定位授权代理方法
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse ||
status == kCLAuthorizationStatusAuthorizedAlways) {
//再重新获取ssid
[self getCurrentWifi];
}
}
- (void)getUserLocation{
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
}
//如果用户第一次拒绝了,触发代理重新选择,要用户打开位置权限
[self.locationManager requestAlwaysAuthorization];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 1.0f;
[self.locationManager startUpdatingLocation];
}