【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 信息 :

  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 版本:

 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];

 

posted @ 2014-09-17 12:45  Danny_吕昌辉  阅读(4782)  评论(0编辑  收藏  举报