本文转载至 http://blog.csdn.net/wbw1985/article/details/20530281
2010年开始苹果清理了一批APP Store上的WIFI扫描软件, 缘由语焉不详.
这些WIFI扫描软件使用了苹果的私有函数apple80211.framework
尽管不能合法(指能通过App Store的审核)的获取WIFI列表, 不过我们还是可以获取到当前Wifi连接的信息,比如SSID.
SSID全称Service Set IDentifier, 即Wifi网络的公开名称.
苹果在IOS v4.1+版本上提供了公开的方法来获取该信息.
示范代码如下:
- #import <SystemConfiguration/CaptiveNetwork.h>
- - (id)fetchSSIDInfo
- {
- NSArray *ifs = (id)CNCopySupportedInterfaces();
- NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
- id info = nil;
- for (NSString *ifnam in ifs) {
- info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
- NSLog(@"%s: %@ => %@", __func__, ifnam, info);
- if (info && [info count]) {
- break;
- }
- [info release];
- }
- [ifs release];
- return [info autorelease];
- }
对于ARC版本, 代码可简化如下:
- - (id)fetchSSIDInfo {
- NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
- NSLog(@"Supported interfaces: %@", ifs);
- id info = nil;
- for (NSString *ifnam in ifs) {
- info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
- NSLog(@"%@ => %@", ifnam, info);
- if (info && [info count]) { break; }
- }
- return info;
- }
参考链接:
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以上版本提供了公开的方法来获取该信息.
- #import <SystemConfiguration/CaptiveNetwork.h>
- -(id)fetchSSIDInfo
- {
- NSArray *ifs = (id)CNCopySupportedInterfaces();
- NSLog(@"%s: Supported interfaces: %@", __func__, ifs);
- id info = nil;
- for (NSString *ifnam in ifs) {
- info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
- if (info && [info count]) {
- break;
- }
- [info release];
- }
- [ifs release];
- return [info autorelease];
- }
- - (NSString *)currentWifiSSID {
- // Does not work on the simulator.
- NSString *ssid = nil;
- NSArray *ifs = ( id)CNCopySupportedInterfaces();
- NSLog(@"ifs:%@",ifs);
- for (NSString *ifnam in ifs) {
- NSDictionary *info = (id)CNCopyCurrentNetworkInfo((CFStringRef)ifnam);
- NSLog(@"dici:%@",[info allKeys]);
- if (info[@"SSIDD"]) {
- ssid = info[@"SSID"];
- }
- }
- return ssid;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 40, 200, 40)];
- tempLabel.textAlignment=NSTextAlignmentCenter;
- [self.view addSubview:tempLabel];
- NSDictionary *ifs = [self fetchSSIDInfo];
- NSString *ssid = [[ifs objectForKey:@"SSID"] lowercaseString];
- tempLabel.text=ssid;
- }
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 版本:
- - (id)fetchSSIDInfo {
- NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
- NSLog(@"Supported interfaces: %@", ifs);
- id info = nil;
- for (NSString *ifnam in ifs) {
- info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
- NSLog(@"%@ => %@", ifnam, info);
- if (info && [info count]) { break; }
- }
- return info;
- }
效果如下: