我需要获取当前连接的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上找到很多文档或任何示例代码示例...我们将非常感谢任何帮助。
答案 0 :(得分:27)
您需要找出可用的网络,然后将它们传递给CNCopyCurrentNetworkInfo。例如:
CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
...然后你可以在你回来的词典(myDict)上使用kCNNetworkInfoKeySSID找出SSID。不要忘记适当地释放/管理记忆。
答案 1 :(得分:23)
iOS 12
您必须从功能启用Access WiFi信息。
重要 要在iOS 12及更高版本中使用此功能,请在Xcode中为您的应用启用Access WiFi信息功能。启用此功能后,Xcode会自动将Access WiFi信息权利添加到您的权利文件和应用程序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。(API Reference)
您需要导入 SystemConfiguration / CaptiveNetwork.h 并将 SystemConfiguration.framework 添加到目标的链接库(在构建阶段)。
以下是swift (RikiRiocma's Answer)中的代码段:
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起,Captive Network已被弃用*。 (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 Developer论坛上谈到这个问题之后发生了逆转。 。
答案 2 :(得分:8)
添加 SystemConfiguration.framework
import &lt; SystemConfiguration / CaptiveNetwork.h&GT; 强>
使用以下方法
+ (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;
}
答案 3 :(得分:2)
请注意,在Xcode 10和iOS 12中,您现在需要启用“访问Wifi信息”功能。