在13以前是可以直接获取用户的SSID的
获取代码如下:
- (NSString *)currentWifiSSID {
NSString *ssid = nil;
NSArray *ifs = (__bridge id)CNCopySupportedInterfaces();
for (NSString *ifname in ifs) {
NSDictionary *info = (__bridge id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifname);
if (info[@"SSID"]) {
ssid = info[@"SSID"];
}
}
return ssid;
}
iOS 13以前 获取的结果如下:
2019/09/26 16:21:37:132 network info -> {
BSSID = "1a:2d:fe:a5:34:54";
SSID = "chinalife";
SSIDDATA = <68262382 7128731f1 43>;
}
但是升级到iOS13以后,也能获取的结果但是是占位的无意义的
2019/09/26 17:01:34:327 network info -> {
BSSID = "00:00:00:00:00:00";
SSID = WLAN;
SSIDDATA = <31a2187b>;
}
原因是因为没有用户授权获取位置信息权限
见苹果原文
Dear Developer,
As we announced at WWDC19, we're making changes to further protect user privacy and prevent unauthorized location tracking. Starting with iOS 13, the CNCopyCurrentNetworkInfo API will no longer return valid Wi-Fi SSID and BSSID information. Instead, the information returned by default will be:
SSID: “Wi-Fi” or “WLAN” (“WLAN" will be returned for the China SKU)
BSSID: "00:00:00:00:00:00"
If your app is using this API, we encourage you to adopt alternative approaches that don’t require Wi-Fi or network information. Valid SSID and BSSID information from CNCopyCurrentNetworkInfo will still be provided to VPN apps, apps that have used NEHotspotConfiguration to configure the current Wi-Fi network, and apps that have obtained permission to access user location through Location Services.
Test your app on the latest iOS 13 beta to make sure it works properly. If your app requires valid Wi-Fi SSID and BSSID information to function, you can do the following:
For accessory setup apps, use the NEHotSpotConfiguration API, which now has the option to pass a prefix of the SSID hotspot your app expects to connect to.
For other types of apps, use the CoreLocation API to request the user’s consent to access location information.
Learn more by reading the updated documentation or viewing the the Advances in Networking session video from WWDC19. You can also submit a TSI for code-level support.
Best regards,
Apple Developer Relations
文章的意思就是 获取WiFi SSID的接口CNCopyCurrentNetworkInfo 不再返回SSID的值 需要先获取用户位置权限才能返回SSID
所以需要加上授权获取位置信息
- 在 info.plist 里加入申请用户位置信息授权
NSLocationAlwaysAndWhenInUseUsageDescription
移动考勤需要使用您的位置信息
NSLocationAlwaysUsageDescription
移动考勤需要使用您的位置信息
NSLocationUsageDescription
移动考勤需要使用您的位置信息
NSLocationWhenInUseUsageDescription
移动考勤需要使用您的位置信息
- 弹窗提示用户进行位置信息授权
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 1.0f;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
- 在获取SSID前检查用户目前的权限并提示
if (!(([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways) || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse))) {
// 弹出 去设置 的窗口
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"请打开app位置信息的授权" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打开app定位设置
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}];
[alertVC addAction:ok];
[self presentViewController:alertVC animated:YES completion:nil];
return;
}