项目作者: HackingGate

项目描述 :
Fix CNCopyCurrentNetworkInfo() does NOT work on iOS13
高级语言: Swift
项目地址: git://github.com/HackingGate/iOS13-WiFi-Info.git
创建时间: 2019-06-12T07:44:09Z
项目社区:https://github.com/HackingGate/iOS13-WiFi-Info

开源协议:MIT License

下载


iOS13-WiFi-Info

Fix CNCopyCurrentNetworkInfo() does NOT work in iOS13 and later

Get Wi-Fi SSID in iOS 12 and earlier

stackoverflow

  1. import Foundation
  2. import SystemConfiguration.CaptiveNetwork
  3. func getSSID() -> String? {
  4. var ssid: String?
  5. if let interfaces = CNCopySupportedInterfaces() as NSArray? {
  6. for interface in interfaces {
  7. if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
  8. ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
  9. break
  10. }
  11. }
  12. }
  13. return ssid
  14. }

To use CNCopyCurrentNetworkInfo() in iOS 12 and later, enable the Access WiFi Information capability in Xcode. For more information, see Access WiFi Information Entitlement.

CNCopyCurrentNetworkInfo() returns nil in iOS 13 and later

Watch WWDC19 Session 713: Advances in Networking, Part 2.

Now you all know the important privacy to Apple. And one of the things we realized. Is that… Accessing Wi-Fi information can be used to infer location.

So starting now, to access that Wi-Fi information. You’ll need the same kind of privileges that you’ll need to get other location information.

WWDC19-CNCopyCurrentNetworkInfo().png.png)

Requires Capability: Access Wi-Fi Information

Must also meet at least one of criteria below

  • Apps with permission to access location
  • Currently enabled VPN app
  • NEHotspotConfiguration (only Wi-Fi networks that the app configured)

Otherwise, returns nil

Get Wi-Fi SSID in iOS 13 and later

Import Core Location framework

  1. import CoreLocation

Function to update UI

  1. func updateWiFi() {
  2. print("SSID: \(currentNetworkInfos?.first?.ssid)")
  3. ssidLabel.text = getSSID()
  4. }

Ask location permission

  1. if #available(iOS 13.0, *) {
  2. let status = CLLocationManager.authorizationStatus()
  3. if status == .authorizedWhenInUse {
  4. updateWiFi()
  5. } else {
  6. locationManager.delegate = self
  7. locationManager.requestWhenInUseAuthorization()
  8. }
  9. } else {
  10. updateWiFi()
  11. }

Implement CLLocationManagerDelegate

  1. class ViewController: UIViewController, CLLocationManagerDelegate {
  2. ...
  3. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  4. if status == .authorizedWhenInUse {
  5. updateWiFi()
  6. }
  7. }
  8. ...
  9. }

Update your app

If your app uses CNCopyCurrentNetworkInfo() and needs to solve the issue. Solve it now. There’s no need to wait for Xcode 11 GM. The solution above is Xcode 10 compatible.