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;
- }
效果如下:
补充:------此方法也未必通过审核------------------
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);
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];
}
|
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/