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

Rob*_*ert 25 iphone cocoa-touch objective-c 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);
Run Code Online (Sandbox Code Playgroud)

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

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

lxt*_*lxt 27

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

CFArrayRef myArray = CNCopySupportedInterfaces();
CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
Run Code Online (Sandbox Code Playgroud)

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

  • 你可以添加整个功能,因为我已经导入了cnnetwork并添加了这段代码.但我的应用程序崩溃在第二行EXC_BAD_ACCESS (2认同)
  • m也面临同样的崩溃问题,我没有得到如何解决这个问题,在我的情况下,myArray的值是nil,m没有从CNCopySupportedInterface()得到任何值; (2认同)

Emi*_*iOS 23

更新iOS 12,快速4.2

iOS 12

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

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

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
    }
}
Run Code Online (Sandbox Code Playgroud)

适用于iOS 10的更新

iOS 10中不再弃用CNCopySupportedInterfaces.(API参考)

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

这是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
    }
}
Run Code Online (Sandbox Code Playgroud)

(重要: 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;
}
Run Code Online (Sandbox Code Playgroud)

适用于iOS 9的更新

截至iOS 9,Captive Network已被弃用*.(来源)

*不再在iOS 10中弃用,请参见上文.

建议你使用NEHotspotHelper(来源)

您需要通过networkextension@apple.com向Apple发送电子邮件并申请权利.(来源)

示例代码(不是我的代码.请参阅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;
}
Run Code Online (Sandbox Code Playgroud)

旁注:是的,他们弃用了iOS 9中的CNCopySupportedInterfaces,并改变了他们在iOS 10中的地位.我与一位Apple网络工程师进行了交谈,之后很多人提交了Radars并在Apple Developer论坛上发表了这个问题.


Dur*_*n.H 8

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

  • 添加SystemConfiguration.framework

  • import <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;
    }
    
    Run Code Online (Sandbox Code Playgroud)