如何获取IOS设备当前链接的SSID
如何获得SSID(服务集标识符),我已经搜索了一些时间,但没有什么有用的。有人能帮忙吗 但是,我在ios7中尝试了这段代码如何获取IOS设备当前链接的SSID,ios,objective-c,wifi,ssid,Ios,Objective C,Wifi,Ssid,如何获得SSID(服务集标识符),我已经搜索了一些时间,但没有什么有用的。有人能帮忙吗 但是,我在ios7中尝试了这段代码 -(NSString *)getWifiName{ NSString *wifiName = @"Not Found"; CFArrayRef myArray = CNCopySupportedInterfaces(); if (myArray != nil) { CFDictionaryRef myDict = CNCopyCurr
-(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。尝试以下方法:
(已编辑)
(在Xcode 8和Swift 3上测试)
首先,您需要添加
@import SystemConfiguration.CaptiveNetwork;
#include <SystemConfiguration/SystemConfiguration.h>
如果要使用swift,则需要将以下代码添加到桥接头中
#include <ifaddrs.h>
#包括
swift(swift 3)的代码为
func fetchSSIDInfo()->字符串{
var currentSSID=“”
如果let接口:CFArray=CNCopySupportedInterfaces(){
对于0中的i..奇怪的是,我直接从我的项目复制粘贴了这段代码,它在那里工作得很好。我认为问题出在另一个地方仍然是错误的,并且由于“CFDictionaryRef networkDetails=CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(interfaces,0));”一句中的“exc_bad_access”而失败这意味着接口数组为空。我在这里编辑了一位代码,您可以在这里阅读有关CNCopySupportedInterfaces函数的内容。我认为这可能与您的wi-fi选项有关。@PavloShadov在iOS9中不推荐使用它
@import SystemConfiguration.CaptiveNetwork;
#include <SystemConfiguration/SystemConfiguration.h>
- (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;}
#include <ifaddrs.h>
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
}