【问题标题】:How to get SSID of currently connected Wifi Network in swift | iOS 14?如何快速获取当前连接的 Wifi 网络的 SSID | iOS 14?
【发布时间】:2021-02-16 21:55:18
【问题描述】:

我需要获取当前连接网络的 ssid。我需要这个的原因是让我的应用程序在连接到特定网络时能够执行某些功能。现在我似乎无法弄清楚如何获得 ssid?我已经在线阅读并实现了以下内容。

-> 允许的用户位置

-> 登录 Apple 开发帐户并启用 Wifi 访问。

我正在使用的功能是

  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  if status == .authorizedAlways || status == .authorizedAlways {
    NEHotspotNetwork.fetchCurrent { hotspotNetwork in
               if let ssid = hotspotNetwork?.ssid {
                   print("SSID is \(ssid)")
               }
           }
        }
   }

但它给出了以下错误

NEHotspotNetwork nehelper 为 Wi-Fi 信息请求发送了无效的结果代码 [5]

我在这里还缺少什么?我还需要添加什么吗?感谢任何帮助!

【问题讨论】:

  • 我收到此错误:类型“NEHotspotNetwork”没有成员“fetchCurrent”
  • 它对我有用,正如我在这里提到的:stackoverflow.com/questions/63130232/… 唯一的问题是,我收到此错误:'' [] NEHotspotNetwork nehelper sent invalid result code [1] for Wi-Fi information request' ' 几秒钟后没有网络数据...

标签: swift5 ios14 nehotspothelper


【解决方案1】:

我已经整理出了获取当前连接的 Wifi 的 SSID 的方法。以下是编写代码之前要遵循的先决条件。

->您必须有一个付费开发者帐户。

->你必须有一个物理设备

-> 您必须通过转到 Target->Signing & Capabilities 并添加 Access WiFi Information 或添加来启用 Wifi-Entitlement <key>com.apple.developer.networking.wifi-info</key> <true/> 直接添加到您的权利文件中。

-> 允许用户访问位置信息

然后在您的班级中使用此代码获取 SSID。

import UIKit
import SystemConfiguration.CaptiveNetwork
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
 
 var locationManager = CLLocationManager()
 var currentNetworkInfos: Array<NetworkInfo>? {
     get {
         return SSID.fetchNetworkInfo()
     }
 }

 
 let ssidLabel:UILabel = {

     let lbl = UILabel()
     lbl.translatesAutoresizingMaskIntoConstraints = false
     return lbl

 }()

 let bssidLabel:UILabel = {
    
     let lbl = UILabel()
     lbl.translatesAutoresizingMaskIntoConstraints = false
     return lbl
     
 }()
 
 override func viewDidLoad() {
     super.viewDidLoad()
     view.backgroundColor = .yellow
     view.addSubview(ssidLabel)
     view.addSubview(bssidLabel)
     
     NSLayoutConstraint.activate([
     
         ssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
         ssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
         
         bssidLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
         bssidLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 20),
     ])
     
     if #available(iOS 13.0, *) {
         let status = CLLocationManager.authorizationStatus()
         if status == .authorizedWhenInUse {
             updateWiFi()
         } else {
             locationManager.delegate = self
             locationManager.requestWhenInUseAuthorization()
         }
     } else {
         updateWiFi()
     }
 }
 
 func updateWiFi() {
     print("SSID: \(currentNetworkInfos?.first?.ssid ?? "")")
     
     if let ssid = currentNetworkInfos?.first?.ssid {
         ssidLabel.text = "SSID: \(ssid)"
     }
     
     if let bssid = currentNetworkInfos?.first?.bssid {
         bssidLabel.text = "BSSID: \(bssid)"
     }
     
 }
 
 func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
     if status == .authorizedWhenInUse {
         updateWiFi()
     }
   }
 
  }

 public class SSID {
 class func fetchNetworkInfo() -> [NetworkInfo]? {
     if let interfaces: NSArray = CNCopySupportedInterfaces() {
         var networkInfos = [NetworkInfo]()
         for interface in interfaces {
             let interfaceName = interface as! String
             var networkInfo = NetworkInfo(interface: interfaceName,
                                           success: false,
                                           ssid: nil,
                                           bssid: nil)
             if let dict = CNCopyCurrentNetworkInfo(interfaceName as CFString) as NSDictionary? {
                 networkInfo.success = true
                 networkInfo.ssid = dict[kCNNetworkInfoKeySSID as String] as? String
                 networkInfo.bssid = dict[kCNNetworkInfoKeyBSSID as String] as? String
             }
             networkInfos.append(networkInfo)
         }
         return networkInfos
     }
     return nil
   }
 }

 struct NetworkInfo {
 var interface: String
 var success: Bool = false
 var ssid: String?
 var bssid: String?
 }

【讨论】:

  • 您需要在 TargetName->Signing&Capabilities 中添加Access WiFi Information 能力。无需在Info.plist中添加任何与WiFi相关的内容。
【解决方案2】:

查看https://developer.apple.com/contact/request/hotspot-helper/

在使用 NEHotspotHelper 之前,你必须先

授予特殊权利 (com.apple.developer.networking.HotspotHelper)

【讨论】:

猜你喜欢
  • 2014-05-27
  • 2020-02-20
  • 2019-03-01
  • 2021-04-11
  • 1970-01-01
  • 2011-06-08
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多