本文主要是介绍关于 iOS 13 CNCopyCurrentNetworkInfo 不再返回Wi-Fi SSID and BSSID,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
黑客技术 点击右侧关注,了解黑客的世界!
Java开发进阶 点击右侧关注,掌握进阶之路!
Linux编程 点击右侧关注,免费入门到精通!
https://juejin.im/post/5d4d1478f265da03e921b7de
写在前面
最近收到了苹果的邮件,说获取WiFi SSID的接口CNCopyCurrentNetworkInfo 不再返回SSID的值。 不仔细看还真会被吓一跳,对物联网的相关APP简直是炸弹。 仔细看邮件还好说明了可以先获取用户位置权限才能返回SSID。
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
解决代码
- (NSString*) getWifiSsid {
if (@available(iOS 13.0, *)) {
//用户明确拒绝,可以弹窗提示用户到设置中手动打开权限
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
NSLog(@"User has explicitly denied authorization for this application, or location services are disabled in Settings.");
//使用下面接口可以打开当前应用的设置页面
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
return nil;
}
CLLocationManager* cllocation = [[CLLocationManager alloc] init];
if(![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
//弹框提示用户是否开启位置权限
[cllocation requestWhenInUseAuthorization];
usleep(50);
//递归等待用户选选择
return [self getWifiSsidWithCallback:callback];
}
}
NSString *wifiName = nil;
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
return nil;
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
NSLog(@"network info -> %@", networkInfo);
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
CFRelease(dictRef);
}
}
CFRelease(wifiInterfaces);
return wifiName;
}
获取结果
- 同意的打印结果
2019/08/09 14:23:47:121 network info -> {
BSSID = "4c:ed:fb:a0:91:e4";
SSID = "Asus_c039";
SSIDDATA = <41737573 5f633033 39>;
}
- 如果用户不获取位置权限的结果
2019/08/09 14:34:01:586 network info -> {
BSSID = "00:00:00:00:00:00";
SSID = WLAN;
SSIDDATA = <574c414e>;
}
- 如果项目本身已经打开位置权限,则可以直接获取。
推荐↓↓↓
长
按
关
注
?【16个技术公众号】都在这里!
涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。
万水千山总是情,点个 “ 在看” 行不行这篇关于关于 iOS 13 CNCopyCurrentNetworkInfo 不再返回Wi-Fi SSID and BSSID的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!