最近收到了苹果开发者发来的一封邮件:大致内容就是从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 无法调用的问题;