如何获取手机中连接的wifi名称?

来源:互联网 发布:搞怪的p图软件 编辑:程序博客网 时间:2024/05/17 01:14

最近还是比较忙的,有时间了就赶紧来给大家分享点东西,今天的代码是如何获取手机中链接的WIFi名称。

已经用demo测试过了,所以就直接上代码喽。


#import "ViewController.h"#import <SystemConfiguration/CaptiveNetwork.h>@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        //打印WiFi名    NSLog(@"%@",[self getWifiName]);}#pragma mark  获取wifi名字- (NSString *)getWifiName{    NSString *wifiName = nil;        CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();        if (!wifiInterfaces) {        return nil;    }        NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;        for (NSString *interfaceName in interfaces) {        CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));                //判断wifi是否正常        if (dictRef) {            //获取关联字典            NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;            NSLog(@"network info -> %@", networkInfo);                        //取出wifi名字            wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];                        CFRelease(dictRef);        }else{            wifiName = @"没有连接wifi";            return wifiName;        }    }        CFRelease(wifiInterfaces);    return wifiName;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end


打印出的效果是这样的:



你看,依旧是方法很简单,加了注释的干货呦~


感谢观看,学以致用更感谢!




0 0
原创粉丝点击