如何获取iOS设备当前连接的SSID

3

如何获取SSID(服务集标识符),我已经搜索了一段时间但没有找到有用的信息。是否有人可以帮助?

然而,我在iOS7中尝试了这段代码

-(NSString *)getWifiName{
    NSString *wifiName = @"Not Found";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil) {
        CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict != nil) {
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict);

            wifiName = [dict valueForKey:@"SSID"];
        }
    }
    NSLog(@"wifiName:%@", wifiName);
    return wifiName;
}

但是它无法获取SSID。

3个回答

3
(在Xcode 8和Swift 3上测试)首先您需要添加:
@import SystemConfiguration.CaptiveNetwork;
#include <SystemConfiguration/SystemConfiguration.h>

那么Objective-C的代码如下:

- (NSString *) getSSID {
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;}

如果您想使用Swift,那么您需要将以下代码添加到桥接头文件中。
#include <ifaddrs.h>

Swift代码(Swift 3)如下:

func fetchSSIDInfo() ->  String {
    var currentSSID = ""
    if let interfaces:CFArray = 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 unsafeInterfaceData != nil {

                let interfaceData = unsafeInterfaceData! as Dictionary!
                currentSSID = ((interfaceData as? [String : AnyObject])?["SSID"])! as! String

            }
        }
    }
    return currentSSID
}

1
尝试这个:

编辑

,保留HTML,不做解释。
- (NSString *)wifiName
{
    NSString *wifiName = @"Not Found";
    CFArrayRef interfaces = CNCopySupportedInterfaces();
    if (interfaces)
    {
        CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));
        if (networkDetails)
        {
            wifiName = (NSString *)CFDictionaryGetValue(networkDetails, kCNNetworkInfoKeySSID);
            CFRelease(networkDetails);
        }
    }

    return wifiName;
}

奇怪,我直接从我的项目中复制粘贴了这段代码,那里它运行得很好。我认为问题在另一个地方。 - Pavlo Shadov
仍然出现错误并且通过“exc_bad_access”在句子“CFDictionaryRef networkDetails = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces, 0));”失败。 - luziqin
这意味着接口数组为空。我稍微编辑了一下代码。 - Pavlo Shadov
在这里,您可以阅读有关CNCopySupportedInterfaces函数的信息 https://developer.apple.com/Library/ios/documentation/SystemConfiguration/Reference/CaptiveNetworkRef/Reference/reference.html#//apple_ref/c/func/CNCopySupportedInterfaces。我认为它可能与您的Wi-Fi选项有关。 - Pavlo Shadov
@PavloShadov 在iOS9中已经被弃用。 - OhadM

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接