iOS 获取Wi-Fi的SSID

来源:互联网 发布:js windows调用firefox 编辑:程序博客网 时间:2024/06/11 16:38

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

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

[html] view plaincopy
  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 plaincopy
  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 plaincopy
  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. }  

  效果如下:


补充:------此方法也未必通过审核------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
+ (id)fetchSSIDInfo
{
    NSArray*ifs = (id)CNCopySupportedInterfaces();
    NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
    idinfo = nil;
    for(NSString*ifnam in ifs) {
        info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
        if(info && [info count]) {
            break;
        }
        [info release];
    }
    [ifs release];
    return[info autorelease];
}

?
1
2
3
4
//check wifi sid
        NSDictionary*ifs = [BaseFunction fetchSSIDInfo];
        NSString*ssid = [[ifs objectForKey:@"SSID"] lowercaseString];
        debug_NSLog(@"ssid:%@",ssid);


记得增加:
#import <SystemConfiguration/CaptiveNetwork.h>
//首先添加框架首先添加框架:SystemConfiguration.framework 


如果你不考虑提交App Store审核问题
可以看看这个项目库
http://code.google.com/p/iphone-wireless/ 

0 0
原创粉丝点击