项目跟wifi有关,所以得用到这个东西。
抱怨下,苹果给的wifi公共接口少得可怜。导致连个wifi都麻烦。android都能做到一键上网了。。。
#import
<SystemConfiguration/CaptiveNetwork.h>
[html]
view plain
copy
- -(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 信息 :
[html]
view plain
copy
- 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 版本:
[html]
view plain
copy
- - (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;
- }