iPhone,iPad如何获取WIFI名称即SSID

本文介绍了一种在iOS设备上获取当前连接Wi-Fi网络名称的方法,利用公开API实现,避免了使用苹果私有库的风险。示例代码展示了如何在iOS应用中获取SSID信息,并附带了ARC版本的简化实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉不详.

这些WIFI扫描软件使用了苹果的私有函数apple80211.framework

尽管不能合法(指能通过App Store的审核)的获取WIFI列表, 不过我们还是可以获取到当前Wifi连接的信息,比如SSID.

SSID全称Service Set IDentifier, 即Wifi网络的公开名称.

苹果在IOS v4.1+版本上提供了公开的方法来获取该信息.

示范代码如下:

[plain] view plain copy
  1. #import<SystemConfiguration/CaptiveNetwork.h>
  2. -(id)fetchSSIDInfo
  3. {
  4. NSArray*ifs=(id)CNCopySupportedInterfaces();
  5. NSLog(@"%s:Supportedinterfaces:%@",__func__,ifs);
  6. idinfo=nil;
  7. for(NSString*ifnaminifs){
  8. info=(id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
  9. NSLog(@"%s:%@=>%@",__func__,ifnam,info);
  10. if(info&&[infocount]){
  11. break;
  12. }
  13. [inforelease];
  14. }
  15. [ifsrelease];
  16. return[infoautorelease];
  17. }

对于ARC版本, 代码可简化如下:

[plain] view plain copy
  1. -(id)fetchSSIDInfo{
  2. NSArray*ifs=(__bridge_transferid)CNCopySupportedInterfaces();
  3. NSLog(@"Supportedinterfaces:%@",ifs);
  4. idinfo=nil;
  5. for(NSString*ifnaminifs){
  6. info=(__bridge_transferid)CNCopyCurrentNetworkInfo((__bridgeCFStringRef)ifnam);
  7. NSLog(@"%@=>%@",ifnam,info);
  8. if(info&&[infocount]){break;}
  9. }
  10. returninfo;
  11. }

参考链接:

1. http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library

2. http://answers.yahoo.com/question/index?qid=20100529040141AAKd8dO



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:Supportedinterfaces:%@",__func__,ifs);
  5. idinfo=nil;
  6. for(NSString*ifnaminifs){
  7. info=(id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
  8. if(info&&[infocount]){
  9. break;
  10. }
  11. [inforelease];
  12. }
  13. [ifsrelease];
  14. return[infoautorelease];
  15. }
  16. -(NSString*)currentWifiSSID{
  17. //Doesnotworkonthesimulator.
  18. NSString*ssid=nil;
  19. NSArray*ifs=(id)CNCopySupportedInterfaces();
  20. NSLog(@"ifs:%@",ifs);
  21. for(NSString*ifnaminifs){
  22. NSDictionary*info=(id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
  23. NSLog(@"dici:%@",[infoallKeys]);
  24. if(info[@"SSIDD"]){
  25. ssid=info[@"SSID"];
  26. }
  27. }
  28. returnssid;
  29. }
  30. -(void)viewDidLoad
  31. {
  32. [superviewDidLoad];
  33. tempLabel=[[UILabelalloc]initWithFrame:CGRectMake(50,40,200,40)];
  34. tempLabel.textAlignment=NSTextAlignmentCenter;
  35. [self.viewaddSubview:tempLabel];
  36. NSDictionary*ifs=[selffetchSSIDInfo];
  37. NSString*ssid=[[ifsobjectForKey:@"SSID"]lowercaseString];
  38. tempLabel.text=ssid;
  39. }


log 信息 :

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

ARC 版本:

[html] view plain copy
  1. -(id)fetchSSIDInfo{
  2. NSArray*ifs=(__bridge_transferid)CNCopySupportedInterfaces();
  3. NSLog(@"Supportedinterfaces:%@",ifs);
  4. idinfo=nil;
  5. for(NSString*ifnaminifs){
  6. info=(__bridge_transferid)CNCopyCurrentNetworkInfo((__bridgeCFStringRef)ifnam);
  7. NSLog(@"%@=>%@",ifnam,info);
  8. if(info&&[infocount]){break;}
  9. }
  10. returninfo;
  11. }

效果如下:


[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:Supportedinterfaces:%@",__func__,ifs);
  5. idinfo=nil;
  6. for(NSString*ifnaminifs){
  7. info=(id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
  8. if(info&&[infocount]){
  9. break;
  10. }
  11. [inforelease];
  12. }
  13. [ifsrelease];
  14. return[infoautorelease];
  15. }
  16. -(NSString*)currentWifiSSID{
  17. //Doesnotworkonthesimulator.
  18. NSString*ssid=nil;
  19. NSArray*ifs=(id)CNCopySupportedInterfaces();
  20. NSLog(@"ifs:%@",ifs);
  21. for(NSString*ifnaminifs){
  22. NSDictionary*info=(id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
  23. NSLog(@"dici:%@",[infoallKeys]);
  24. if(info[@"SSIDD"]){
  25. ssid=info[@"SSID"];
  26. }
  27. }
  28. returnssid;
  29. }
  30. -(void)viewDidLoad
  31. {
  32. [superviewDidLoad];
  33. tempLabel=[[UILabelalloc]initWithFrame:CGRectMake(50,40,200,40)];
  34. tempLabel.textAlignment=NSTextAlignmentCenter;
  35. [self.viewaddSubview:tempLabel];
  36. NSDictionary*ifs=[selffetchSSIDInfo];
  37. NSString*ssid=[[ifsobjectForKey:@"SSID"]lowercaseString];
  38. tempLabel.text=ssid;
  39. }


log 信息 :

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

ARC 版本:

[html] view plain copy
  1. -(id)fetchSSIDInfo{
  2. NSArray*ifs=(__bridge_transferid)CNCopySupportedInterfaces();
  3. NSLog(@"Supportedinterfaces:%@",ifs);
  4. idinfo=nil;
  5. for(NSString*ifnaminifs){
  6. info=(__bridge_transferid)CNCopyCurrentNetworkInfo((__bridgeCFStringRef)ifnam);
  7. NSLog(@"%@=>%@",ifnam,info);
  8. if(info&&[infocount]){break;}
  9. }
  10. returninfo;
  11. }

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值