【问题标题】:How to fetch SSID in iOS device with iOS 13如何使用 iOS 13 在 iOS 设备中获取 SSID
【发布时间】:2020-10-16 02:34:21
【问题描述】:

我有一个 Objective-C iPhone 应用程序,目前我正在使用下面的代码来获取连接的 Wifi 名称。但它在 iOS 13 中不起作用。如何在 iOS 13 中获取连接的 Wifi SSID?

目前我在 Swift 中使用以下代码:

public class SSID {
class func fetch() -> String {
    var currentSSID = ""
    if let interfaces = CNCopySupportedInterfaces() {
        for i in 0..<CFArrayGetCount(interfaces) {
            let interfaceName = CFArrayGetValueAtIndex(interfaces, i)
            let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
            let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
            if let interfaceData = unsafeInterfaceData as? [String: AnyObject] {
                currentSSID = interfaceData["SSID"] as! String
                let BSSID = interfaceData["BSSID"] as! String
                let SSIDDATA = interfaceData["SSIDDATA"] as! String
                debugPrint("ssid=\(currentSSID), BSSID=\(BSSID), SSIDDATA=\(SSIDDATA)")
            }
        }
    }
    return currentSSID
  }
}

但此代码在 iOS 13 中返回 nil,提前致谢!

【问题讨论】:

    标签: ios swift iphone ios13 ssid


    【解决方案1】:

    使用 iOS 14 上提供的代码我收到以下错误:

    nehelper sent invalid result code [1] for Wi-Fi information request

    搜索那个错误把我带到了这个question

    Solution:

    请求的应用程序必须满足以下要求之一:

    应用使用Core Location,并拥有用户授权使用 位置信息。

    应用程序使用 NEHotspotConfiguration API 来配置当前 无线网络。

    该应用已安装有效的 VPN 配置。

    未能满足上述任何要求的应用会​​收到 以下返回值:

    与 iOS 12 或更早版本关联的应用会收到一个字典,其中包含 伪值。在这种情况下,SSID 是 Wi-Fi(或中国的 WLAN 地区),BSSID 为 00:00:00:00:00:00。

    链接到 iOS 13 或更高版本的应用收到 NULL。

    重要

    要使用此功能,与 iOS 12 或更高版本关联的应用程序必须 在 Xcode 中启用 Access WiFi Information 功能。

    我还确认,一旦您请求位置许可,这实际上适用于 iOS 14。

    import CoreLocation
    import UIKit
    import SystemConfiguration.CaptiveNetwork
    
    final class ViewController: UIViewController {
      var locationManager: CLLocationManager?
    
      override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager = CLLocationManager()
        locationManager?.delegate = self
        locationManager?.requestAlwaysAuthorization()
      }
    
      func getWiFiName() -> String? {
        var ssid: String?
    
        if let interfaces = CNCopySupportedInterfaces() as NSArray? {
          for interface in interfaces {
            if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
              ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
              break
            }
          }
        }
    
        return ssid
      }
    }
    
    extension ViewController: CLLocationManagerDelegate {
      func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedAlways || status == .authorizedAlways {
          let ssid = self.getWiFiName()
          print("SSID: \(String(describing: ssid))")
        }
      }
    }
    

    输出:SSID: YaMomsWiFi

    不要忘记在您的 plist 中包含 wifi 授权和必要的密钥以获得位置许可。

    【讨论】:

    猜你喜欢
    • 2020-02-01
    • 2020-01-21
    • 2014-10-05
    • 2021-01-02
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多