【问题标题】:How do I use CaptiveNetwork to get the current WiFi Hotspot Name如何使用 CaptiveNetwork 获取当前 WiFi 热点名称
【发布时间】:2011-06-10 09:32:16
【问题描述】:

我需要获取当前连接的 Wi-Fi 热点的名称,例如“BT OpenZone”

我被告知可以使用CaptiveNetwork 特别是 CNCopyCurrentNetworkInfo 来完成

到目前为止我的代码:

#import <SystemConfiguration/CaptiveNetwork.h>
...

// Get the dictionary containing the captive network infomation
CFDictionaryRef captiveNtwrkDict = CNCopyCurrentNetworkInfo(kCNNetworkInfoKeySSID);

// Get the count of the key value pairs to test if it has worked
int count = CFDictionaryGetCount(captiveNtwrkDict);
NSLog(@"Count of dict:%d",count);

当代码在 WiFi 热点中的设备上运行时,captiveNtwrkDict 为零。

有没有人设法让它工作?我在 CaptiveNetworks 上找不到太多文档或任何示例代码示例...任何帮助将不胜感激。

【问题讨论】:

    标签: iphone objective-c cocoa-touch wifi


    【解决方案1】:

    您需要找出可用的网络,然后将它们传递给 CNCopyCurrentNetworkInfo。例如:

    CFArrayRef myArray = CNCopySupportedInterfaces();
    CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
    

    ...然后您可以使用返回的字典 (myDict) 上的 kCNNetworkInfoKeySSID 来找出 SSID。不要忘记适当地释放/管理内存。

    【讨论】:

    • 您能否添加整个功能,因为我已经导入了 cnnetwork 并添加了此代码。但我的应用程序在第二行 EXC_BAD_ACCESS 上崩溃了
    • m 也面临同样的崩溃问题,m 没有得到如何解决这个问题,在我的情况下,myArray 的值为 nil,m 没有从 CNCopySupportedInterface() 获得任何值;
    【解决方案2】:

    iOS 12 的更新,swift 4.2

    iOS 12

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

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

    Swift4.2

    public class SSID {
        class func fetchSSIDInfo() -> String {
            var currentSSID = ""
            if let interfaces = CNCopySupportedInterfaces() {
                for i in 0..<CFArrayGetCount(interfaces) {
                    let interfaceName: UnsafeRawPointer = 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"]
                        debugPrint("ssid=\(currentSSID), BSSID=\(BSSID), SSIDDATA=\(SSIDDATA)")
                    }
                }
            }
            return currentSSID
        }
    }
    

    iOS 10 更新

    CNCopySupportedInterfaces 在 iOS 10 中不再被弃用。(API Reference)

    您需要导入 SystemConfiguration/CaptiveNetwork.h 并将 SystemConfiguration.framework 添加到目标的链接库(在构建阶段)。

    这是 swift (RikiRiocma's Answer) 中的代码 sn-p:

    import Foundation
    import SystemConfiguration.CaptiveNetwork
    
    public class SSID {
        class func fetchSSIDInfo() ->  String {
            var currentSSID = ""
            if let interfaces:CFArray! = CNCopySupportedInterfaces() {
                for i in 0..<CFArrayGetCount(interfaces){
                    let interfaceName: UnsafePointer<Void> = CFArrayGetValueAtIndex(interfaces, i)
                    let rec = unsafeBitCast(interfaceName, AnyObject.self)
                    let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
                    if unsafeInterfaceData != nil {
                        let interfaceData = unsafeInterfaceData! as Dictionary!
                        currentSSID = interfaceData["SSID"] as! String
                    }
                }
            }
        return currentSSID
        }
    }
    

    重要提示: CNCopySupportedInterfaces 在模拟器上返回 nil。)

    对于 Objective-c,请参阅Esad's answer here and below

    + (NSString *)GetCurrentWifiHotSpotName {    
        NSString *wifiName = nil;
        NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
        for (NSString *ifnam in ifs) {
            NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
            if (info[@"SSID"]) {
                wifiName = info[@"SSID"];
            }
        }
        return wifiName;
    }
    

    iOS 9 更新

    自 iOS 9 起,强制网络已弃用*。 (source)

    *在 iOS 10 中不再被弃用,见上文。

    建议您使用 NEHotspotHelper (source)

    您需要通过 networkextension@apple.com 向 Apple 发送电子邮件并请求授权。 (source)

    示例代码 (Not my code. See Pablo A's answer):

    for(NEHotspotNetwork *hotspotNetwork in [NEHotspotHelper supportedNetworkInterfaces]) {
        NSString *ssid = hotspotNetwork.SSID;
        NSString *bssid = hotspotNetwork.BSSID;
        BOOL secure = hotspotNetwork.secure;
        BOOL autoJoined = hotspotNetwork.autoJoined;
        double signalStrength = hotspotNetwork.signalStrength;
    }
    

    旁注:是的,他们在 iOS 9 中弃用了 CNCopySupportedInterfaces,并在 iOS 10 中颠倒了他们的立场。我与一位 Apple 网络工程师交谈过,在这么多人提交 Radars 并在 Apple 开发者论坛上谈论这个问题之后,他们的立场发生了逆转.

    【讨论】:

    • 感谢您的彻底回复,NEHotspotHelper 代码无法在 swift 3 上运行
    • 是否需要在swift代码中添加release/retain?
    【解决方案3】:

    简单好用的代码sn-p(方法):

    • 添加SystemConfiguration.framework

    • 导入

    • 使用下面的方法

      + (NSString *)GetCurrentWifiHotSpotName {
      
          NSString *wifiName = nil;
          NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
          for (NSString *ifnam in ifs) {
              NSDictionary *info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
      
              NSLog(@"info:%@",info);
      
              if (info[@"SSID"]) {
                  wifiName = info[@"SSID"];
              }
          }
          return wifiName;
      }
      

    【讨论】:

    • 你的方法就像一个魅力。我们可以通过自己的 App 禁用/启用 Wifi 吗?
    • 我们可以启用/禁用 wifi 吗?
    • 如果您的应用不适合应用商店,您可以通过在跳板中挂钩 SBWiFiManager 类来了解更多信息,请访问stackoverflow.com/a/22590416/730807
    • @Duraiamuthan.H 这是否也适用于模拟器,或者仅在实际设备上运行时?
    • @RoyH 我没有在模拟器中测试过,我想是实际设备
    【解决方案4】:

    请注意,在 Xcode 10 和 iOS 12 中,您现在需要启用“访问 Wifi 信息”功能。

    来源:https://openradar.appspot.com/43280182

    【讨论】:

      猜你喜欢
      • 2012-11-05
      • 1970-01-01
      • 2015-10-11
      • 2019-03-01
      • 2012-08-04
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多