最近在做测速SDK开发,对iOS端wifi进行了调研,分享一下调研成果
从 iOS 4.1 开始,Apple 就提供了「CNCopyCurrentNetworkInfo」这项函数,调用时将会得到 SSID 与 BSSID等信息。 iOS 12 开始开发者必须在Xcode开启Access WiFi Information
才能返回wifi信息。而在 iOS 13后,条件将变得更为严格,又增加了开启定位权限。
iOS13获取Wi-Fi信息步骤(必须真机调式)
-
info.plist
中添加
<key>NSLocationWhenInUseUsageDescription</key>
<string>获取wifi信息需要开启定位权限</string>
-
Xcode开启Access WiFi Information权限
- 引用SystemConfiguration.framework
//导入头文件
#import <SystemConfiguration/SystemConfiguration.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <CoreLocation/CoreLocation.h>
- (NSDictionary *)getWifiInfo{
// if (@available(iOS 13.0, *)) {
// 检查定位权限
// if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
// }
// CLLocationManager* cllocation = [[CLLocationManager alloc] init];
// if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
// [cllocation requestWhenInUseAuthorization];
// }
// }
NSDictionary *info = nil;
NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
if (ifs.count == 0) {
return nil;
}
for (NSString *ifnam in ifs) {
info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
}
return info;
}
输出
{
BSSID = "";
SSID = "";
SSIDDATA = {length = , bytes = };
}