#import <SystemConfiguration/CaptiveNetwork.h>
-(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;
}
网友评论