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

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


你可能感兴趣的:(iOS)