iOS-获取手机连接的WiFi信息

  • 1、引入头文件
#import <SystemConfiguration/CaptiveNetwork.h>
  • 2、获取到WiFi的信息,返回值为WiFi名字
-(void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(20, 100, 280, 50);
    btn.backgroundColor = [UIColor blueColor];
    [self.view addSubview:btn];
    [btn setTitle:@"查找WIFI" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(createButton:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)createButton:(UIButton *)button
{
    [button setTitle:[NSString stringWithFormat:@"WIFI名字:%@", [self fetchSSIDInfo]] forState:UIControlStateNormal];
}
-(NSString *)fetchSSIDInfo
{
    NSString *currentSSID = @"Not Found";
    CFArrayRef myArray = CNCopySupportedInterfaces();
    if (myArray != nil){
        NSDictionary* myDict = (__bridge NSDictionary *) CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
        if (myDict!=nil){
            currentSSID=[myDict valueForKey:@"SSID"];
/* myDict打印:{
       BSSID = "8a:25:93:57:14:24";
       SSID = sichou;
       SSIDDATA = <73696368 6f75>;
       }
*/
        } else {
            currentSSID=@"<<NONE>>";
        }
    } else {
        currentSSID=@"<<NONE>>";
    }
//    NSDictionary *info = nil;
//    for (NSString *ifnam in ifs) {
//        info = (__bridge NSDictionary *)CNCopyCurrentNetworkInfo((CFStringRef)CFBridgingRetain(ifnam));
//    }
    CFRelease(myArray);
    return currentSSID;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容