捕获所有Wi-Fi网络并保存到列表中

时间:2013-08-30 16:54:45

标签: objective-c macos cocoa network-protocols

使用CoreWLAN Cocoa Library获取Wi-Fi网络

我正在尝试使用CoreWLAN库获取当前mac可以看到的Wi-Fi网络列表,但无济于事。我一直在他们的网站上使用Apple's example

我正在寻找的是一个列表(数组):

  • 可用网络名称
  • 他们的密码/加密类型

以下是我的代码,现在显然已被弃用,在我NSLog("%@", currentNetworkName)时什么都不做:

self.currentInterface = [CWInterface interfaceWithName:[CWInterface supportedInterfaces];
NSString *currentNetworkName = nil;
currentNetworkSSID =  [self.currentInterface ssid];

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以使用CoreWLAN Framework。

以下是一些在Mavericks上测试的示例代码(10.9.1)

CWInterface* interface = [[CWInterface alloc] initWithInterfaceName:@"en1"];
    NSError* error;
    NSSet* wifi = [interface scanForNetworksWithName:nil error:&error];
    if (error) {
        NSLog(@"%@", [error description]);
    } else {
        [wifi enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
            CWNetwork* net = (CWNetwork*)obj;
            NSLog(@"%@", net.ssid);
         }
    }

CWNetwork类有一个检查安全性的方法:

- (BOOL)supportsSecurity:(CWSecurity)security;

使用CWSecurity:

enum {
   kCWSecurityNone = 0,
   kCWSecurityWEP = 1,
   kCWSecurityWPAPersonal = 2,
   kCWSecurityWPAPersonalMixed = 3,
   kCWSecurityWPA2Personal = 4,
   kCWSecurityPersonal = 5,
   kCWSecurityDynamicWEP = 6,
   kCWSecurityWPAEnterprise = 7,
   kCWSecurityWPAEnterpriseMixed = 8,
   kCWSecurityWPA2Enterprise = 9,
   kCWSecurityEnterprise = 10,
   kCWSecurityUnknown = NSIntegerMax,
};