iOS 获取wifi ssid 名称

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

#import 

-(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;
}

  效果如下:

iOS 获取wifi ssid 名称_第1张图片


你可能感兴趣的:(iphone开发学习和总结)