iOS 获取wifi ssid 名称

SSID全称Service Set IDentifier, 即Wifi网络的公开名称.在IOS 4.1以上版本提供了公开的方法来获取该信息.

[html]  view plain copy
  1. #import <SystemConfiguration/CaptiveNetwork.h>  

[html]  view plain copy
  1. -(id)fetchSSIDInfo  
  2. {  
  3.     NSArray *ifs = (id)CNCopySupportedInterfaces();  
  4.     NSLog(@"%s: Supported interfaces: %@", __func__, ifs);  
  5.     id info = nil;  
  6.     for (NSString *ifnam in ifs) {  
  7.         info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);  
  8.         if (info && [info count]) {  
  9.             break;  
  10.         }  
  11.         [info release];  
  12.     }  
  13.     [ifs release];  
  14.     return [info autorelease];  
  15. }  
  16.   
  17. - (NSString *)currentWifiSSID {  
  18.     // Does not work on the simulator.  
  19.     NSString *ssid = nil;  
  20.     NSArray *ifs = (  id)CNCopySupportedInterfaces();  
  21.     NSLog(@"ifs:%@",ifs);  
  22.     for (NSString *ifnam in ifs) {  
  23.         NSDictionary *info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);  
  24.         NSLog(@"dici:%@",[info  allKeys]);  
  25.         if (info[@"SSIDD"]) {  
  26.             ssid = info[@"SSID"];  
  27.               
  28.         }  
  29.     }  
  30.     return ssid;  
  31. }  
  32.   
  33. - (void)viewDidLoad  
  34. {  
  35.     [super viewDidLoad];  
  36.       
  37.     tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)];  
  38.     tempLabel.textAlignment=NSTextAlignmentCenter;  
  39.     [self.view addSubview:tempLabel];  
  40.     NSDictionary *ifs = [self fetchSSIDInfo];  
  41.     NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];  
  42.     tempLabel.text=ssid;  
  43.    
  44. }  


log 信息 :

[html]  view plain copy
  1. 2013-06-05 21:39:14.357 wifiNameDemo[9877:707] dici:{  
  2.     BSSID = "f4:ec:38:40:cc:e8";  
  3.     SSID = "Nice_Apple";  
  4.     SSIDDATA = <4e696365 5f417070 6c65>;  
  5. }  
  6. 2013-06-05 21:39:14.360 wifiNameDemo[9877:707] Nice_Apple  

 ARC 版本:

[html]  view plain copy
  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.         info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);  
  7.         NSLog(@"%@ => %@", ifnam, info);  
  8.         if (info && [info count]) { break; }  
  9.     }  
  10.     return info;  
  11. }  

  效果如下:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值