如何使用CaptiveNetwork获取当前的WiFi热点名称

我需要获取当前连接的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为零。

有没有人设法得到它的工作? 我无法findCaptiveNetworks上的文档或任何示例代码示例…任何帮助将不胜感激。

您需要找出哪些networking可用,然后将它们传递到CNCopyCurrentNetworkInfo。 例如:

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

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

更新为iOS 10

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

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

这是一个swift代码片段(RikiRiocma的答案) :

 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在这里和下面的回答

 + (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俘虏networking已弃用*。 ( 来源 )

*不再在iOS 10中弃用,请参阅上文。

build议您使用NEHotspotHelper( 来源 )

您将需要通过networkextension@apple.com通过电子邮件向苹果发送电子邮件并请求授权。 ( 来源 )

示例代码( 不是我的代码,见Pablo A的答案 ):

 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中颠覆了自己的位置。我跟一位苹果networking工程师交谈过,之后就有了这么多人提交了Radars,并在Apple Developer论坛上发表了这个问题。

易于使用的代码片段(方法):

  • 添加SystemConfiguration.framework

  • 导入<SystemConfiguration / CaptiveNetwork.h>

  • 使用下面的方法

     + (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; }