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

标签 ios swift

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

最佳答案

iOS 12

您必须启用从功能中访问 WiFi 信息。

Important To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App 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
}

关于ios - 如何使用 swift 在 iOS 中获取可用的 wifi 网络名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715055/

相关文章:

ios - 在 iOS 中聚类 Google map 标记

ios - 为什么我的 .plist 没有显示保存的数据?

ios - 快速填充 TableView

ios - Swift 将字节数组转换为字符串

Swift:带有 switch 语句的嵌套枚举

swift - 在写入事务之外修改对象

ios - 滚动 UITableView 以显示选定的单元格

ios - 如何减去日期成分?

ios - 方向在 iOS 10 中不旋转,但在 iOS 11 中旋转

ios - 为什么我不能将图像上传到 Parse?有人知道这样做的不同方法吗?