gpt4 book ai didi

ios - 如何使用 iOS 13 在 iOS 设备中获取 SSID

转载 作者:行者123 更新时间:2023-12-01 19:31:23 24 4
gpt4 key购买 nike

我有一个 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 14 上提供的代码,我收到以下错误:

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

搜索该错误将我带到了这个 question

Solution :

The requesting app must meet one of the following requirements:

The app uses Core Location, and has the user’s authorization to uselocation information.

The app uses the NEHotspotConfiguration API to configure the currentWi-Fi network.

The app has active VPN configurations installed.

An app that fails to meet any of the above requirements receives thefollowing return value:

An app linked against iOS 12 or earlier receives a dictionary withpseudo-values. In this case, the SSID is Wi-Fi (or WLAN in the Chinaregion), and the BSSID is 00:00:00:00:00:00.

An app linked against iOS 13 or later receives NULL.

Important

To use this function, an app linked against iOS 12 or later mustenable the Access WiFi Information capability in Xcode.

我还确认,一旦您请求位置许可,这实际上适用于 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 授权和必要的 key 以获取位置权限。

关于ios - 如何使用 iOS 13 在 iOS 设备中获取 SSID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62578677/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com