iphone ipad 获取wifi信息 BSSID SSID

首先,app如果要上app store是没有办法获取wifi列表的~ 

这些WIFI扫描软件使用了苹果的私有函数apple80211.framework 


尽管不能合法(指能通过App Store的审核)的获取WIFI列表, 不过我们还是可以获取到当前Wifi连接的信息,比如SSID. BSSID
SSID全称Service Set IDentifier, 即Wifi网络的公开名称. 

BSSID 是指站点的 MAC 地址.

苹果在IOS v4.1+版本上提供了公开的方法来获取该信息. 

注意记得要在build phases中添加库SystemConfiguration.framework

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

#import <SystemConfiguration/CaptiveNetwork.h>

- (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);  //单个数据info[@"SSID"]; info[@"BSSID"];
         if (info && [info count]) { break; }  
     }  
     return info;  
}

输出结果

2013-09-05 10:32:00.655 wifiTest[3534:707] -[wifiTestAppDelegate fetchSSIDInfo]: Supported interfaces: (  
    en0  
)  
2013-09-05 10:32:00.661 wifiTest [3534:707] -[wifiTestAppDelegate fetchSSIDInfo]: en0 => {  
    BSSID = "0:4:c3:a1:2b:22";  
    SSID = wifiname;  
    SSIDDATA = <01234567 76543210>;  
}

BSSID 正确格式是 00:04:c3:a1:2b:22 但是输出以后却是0:4:c3:a1:2b:22 少了头0,可用一下方法补0


- (NSString *)standardFormateMAC:(NSString *)MAC {
    NSArray * subStr = [MAC componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":-"]];
    NSMutableArray * subStr_M = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSString * str in subStr) {
        if (1 == str.length) {
            NSString * tmpStr = [NSString stringWithFormat:@"0%@", str];
            [subStr_M addObject:tmpStr];
        } else {
            [subStr_M addObject:str];
        }
    }
    
    NSString * formateMAC = [subStr_M componentsJoinedByString:@":"];
    [subStr_M release];
    return [formateMAC uppercaseString];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值