最近还是比较忙的,有时间了就赶紧来给大家分享点东西,今天的代码是如何获取手机中链接的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
打印出的效果是这样的:
感谢观看,学以致用更感谢!