CNCopyCurrentNetworkInfo 在 iOS 13 中的新特性

最近收到了苹果开发者发来的一封邮件:大致内容就是从iOS13开始,将对app中通过CNCopyCurrentNetworkInfo API来获取设备当前所连接的wifi 的功能进行限制;

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

从 iOS 4.1 开始,Apple 就提供了「CNCopyCurrentNetworkInfo」这项函数,调用时将会得到 SSID 与 BSSID; iOS 12 开始,调用该函数将默认返回 nil,需要在 Xcode 项目中开启「Access WiFi Information」后才会返回正确的值。这个功能需要在开发者页面的 App IDs 中激活才能使用。
而在 iOS 13 中,使用这项函数的条件将变得更为严格。根据 WWDC19 Session 713(https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo)的说明,在 iOS 13 中除了开启「Access WiFi Information」以外,App 还需要符合下列三项条件中的至少一项才会返回正确的 CNCopyCurrentNetworkInfo 函数值,否则仍然会返回 nil :

- The app uses Core Location, and has the user’s authorization to use location information.
- The app uses the NEHotspotConfiguration API to configure the current Wi-Fi network.
- The app has active VPN configurations installed.

即:
1、使用定位功能,并且获得了定位服务权限的应用;
2、使用NEHotspotConfiguration配置过的Wi-Fi;
3、应用程序已安装有效的VPN配置;

这里的 NEHotspotConfiguration 是在 iOS 11 中新加入的一个 class,它的特性简单来说是App 将已知 Wi-Fi 的 SSID、密码等信息加入到 App 内的配置文件后,这个 App 可以直接在应用内完成连接至该 Wi-Fi 网路的操作,不需要再跳转至系统偏好设置。简单理解就是:如果当前设备所连接的Wi-Fi不是通过我们的app使用NEHotspotConfiguration来配置的,那么也同样没法使用NEHotspotConfiguration来获取当前Wi-Fi的信息;

对于大部分的app来说,可以通过使用定位功能,只需要写好获取定位服务权限的代码就可以解决 CNCopyCurrentNetworkInfo 无法调用的问题;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值