如何使用swift在iOS中获取可用的wifi网络名称

Tot*_*Gul 19 ios swift

我想获得一个地区的所有WiFi网络及其SSID值.但问题是如何使所有WiFi网络的SSID可用,即使我没有连接到一个.

Ada*_*lon 14

iOS 12

您必须从功能启用Access WiFi信息.

重要信息要在iOS 12及更高版本中使用此功能,请在Xcode中为您的应用启用Access WiFi信息功能.启用此功能后,Xcode会自动将Access WiFi信息权利添加到您的权利文件和应用程序ID中.文档链接

第一;

import SystemConfiguration.CaptiveNetwork

然后;

 func getInterfaces() -> Bool {
    guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
        print("this must be a simulator, no interfaces found")
        return false
    }
    guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
        print("System error: did not come back as array of Strings")
        return false
    }
    for interface in swiftInterfaces {
        print("Looking up SSID info for \(interface)") // en0
        guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else {
            print("System error: \(interface) has no information")
            return false
        }
        guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
            print("System error: interface information is not a string-keyed dictionary")
            return false
        }
        for d in SSIDDict.keys {
            print("\(d): \(SSIDDict[d]!)")
        }
    }
    return true
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎只能在iPhone 7上至少在iOS 11.2.2上返回当前连接的wifi (8认同)

Leo*_*tim 0

这是我的类,打印 WIFI 网络名称

import UIKit
import Foundation
import SystemConfiguration.CaptiveNetwork

class FirstView: UIViewController
{
    @IBOutlet weak var label: UILabel!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        let ssid = self.getWiFiName()
        print("SSID: \(ssid)")
    }

    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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎只显示连接的接口 SSID。 (8认同)