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

时间:2015-07-30 04:12:55

标签: ios swift

我希望获得某个地区的所有WiFi网络及其SSID值。但问题是如何使所有WiFi网络的SSID可用,即使我没有连接到它。

3 个答案:

答案 0 :(得分:12)

iOS 12

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

  

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

首先;

导入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
}

答案 1 :(得分:1)

这是我的班级打印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
    }
}

答案 2 :(得分:0)

是的,可以列出附近的所有WiFi网络。您需要在https://developer.apple.com/contact/network-extension填写调查表,然后可以使用NEHotspotHelper返回热点列表。技术问答https://developer.apple.com/library/archive/qa/qa1942/_index.html