【IOS】获取wifi名称 (即SSID)
iOS 获取wifi ssid 名称
SSID全称Service Set IDentifier, 即Wifi网络的公开名称.在IOS 4.1以上版本提供了公开的方法来获取该信息.
首先添加框架:SystemConfiguration.framework
1 #import <SystemConfiguration/CaptiveNetwork.h> 2 - (id)fetchSSIDInfo 3 { 4 NSArray *ifs = (id)CNCopySupportedInterfaces(); 5 NSLog(@"%s: Supported interfaces: %@", __func__, ifs); 6 id info = nil; 7 for (NSString *ifnam in ifs) 8 { 9 info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam); 10 NSLog(@"%s: %@ => %@", __func__, ifnam, info); 11 if (info && [info count]) 12 { 13 break; 14 } 15 [info release]; 16 } 17 [ifs release]; 18 return [info autorelease]; 19 } 20 21 22 - (NSString *)currentWifiSSID { 23 // Does not work on the simulator. 24 NSString *ssid = nil; 25 NSArray *ifs = ( id)CNCopySupportedInterfaces(); 26 NSLog(@"ifs:%@",ifs); 27 for (NSString *ifnam in ifs) { 28 NSDictionary *info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam); 29 NSLog(@"dici:%@",[info allKeys]); 30 if (info[@"SSIDD"]) { 31 ssid = info[@"SSID"]; 32 33 } 34 } 35 return ssid; 36 } 37 38 - (void)viewDidLoad 39 { 40 [super viewDidLoad]; 41 42 tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)]; 43 tempLabel.textAlignment=NSTextAlignmentCenter; 44 [self.view addSubview:tempLabel]; 45 NSDictionary *ifs = [self fetchSSIDInfo]; 46 NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString]; 47 tempLabel.text=ssid; 48 49 }
log 信息 :
- 2013-06-05 21:39:14.357 wifiNameDemo[9877:707] dici:{
- BSSID = "f4:ec:38:40:cc:e8";
- SSID = "Nice_Apple";
- SSIDDATA = <4e696365 5f417070 6c65>;
- }
- 2013-06-05 21:39:14.360 wifiNameDemo[9877:707] Nice_Apple
ARC 版本:
1 - (id)fetchSSIDInfo { 2 NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces(); 3 NSLog(@"Supported interfaces: %@", ifs); 4 id info = nil; 5 for (NSString *ifnam in ifs) 6 { 7 info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam); 8 NSLog(@"%@ => %@", ifnam, info); 9 if (info && [info count]) 10 { 11 break; 12 } 13 } 14 return info; 15 }
效果如下:
iOS 取得WIFI的热点名称和MAC地址
1 #import <SystemConfiguration/CaptiveNetwork.h> 2 3 NSString *ssid = @"Not Found"; 4 5 NSString *macIp = @"Not Found"; 6 CFArrayRef myArray = CNCopySupportedInterfaces(); 7 if (myArray != nil) { 8 CFDictionaryRef myDict = CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0)); 9 if (myDict != nil) { 10 NSDictionary *dict = (NSDictionary*)CFBridgingRelease(myDict); 11 ssid = [dict valueForKey:@"SSID"]; 12 macIp = [dict valueForKey:@"BSSID"]; 13 } 14 } 15 UIAlertView *av = [[UIAlertView alloc] initWithTitle:ssid 16 message:macIp 17 delegate:nil 18 cancelButtonTitle:nil 19 otherButtonTitles:@"OK", nil]; 20 [av show];
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。